Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
bayhaqi:3801Abū ʿAbdullāh al-Ḥāfiẓ And ʾAbū Bakr Aḥmad b. al-Ḥasan al-Qāḍī Waghayruhumā > Abū al-ʿAbbās Muḥammad b. Yaʿqūb > Baḥr b. Naṣr > Quriʾ > Ibn Wahb Akhbarak Mālik b. Anas Wadāwd b. Qays Wahishām b. Saʿd > Zayd b. Aslam Ḥaddathahum > ʿAṭāʾ b. Yasār

[Machine] The Messenger of Allah ﷺ said: "If anyone of you has doubts about his prayer, he should not know how many times he prayed three or four (rak'ahs). He should stand up and pray a rak'ah, then prostrate twice while sitting before the salutation. If the rak'ah he prayed was the fifth, he should make up for it with these two prostrations. If it was the fourth, then the two prostrations are a deterrent for Satan, except that Hisham reported to Abu Sa'eed al-Khudri that it is done like this."  

البيهقي:٣٨٠١أَخْبَرَنَا أَبُو عَبْدِ اللهِ الْحَافِظُ وَأَبُو بَكْرٍ أَحْمَدُ بْنُ الْحَسَنِ الْقَاضِي وَغَيْرُهُمَا قَالُوا ثنا أَبُو الْعَبَّاسِ مُحَمَّدُ بْنُ يَعْقُوبَ ثنا بَحْرُ بْنُ نَصْرٍ قَالَ قُرِئَ عَلَى ابْنِ وَهْبٍ أَخْبَرَكَ مَالِكُ بْنُ أَنَسٍ وَدَاوُدُ بْنُ قَيْسٍ وَهِشَامُ بْنُ سَعْدٍ أَنَّ زَيْدَ بْنَ أَسْلَمَ حَدَّثَهُمْ عَنْ عَطَاءِ بْنِ يَسَارٍ

أَنَّ رَسُولَ اللهِ ﷺ قَالَ إِذَا شَكَّ أَحَدُكُمْ فِي الصَّلَاةِ فَلَا يَدْرِي كَمْ صَلَّى ثَلَاثًا أَوْ أَرْبَعًا فَلْيَقُمْ فَلْيُصَلِّ رَكْعَةً ثُمَّ لِيَسْجُدْ سَجْدَتَيْنِ وَهُوَ جَالِسٌ قَبْلَ السَّلَامِ فَإِنْ كَانَتِ الرَّكْعَةُ الَّتِي صَلَّى خَامِسَةً شَفَعَهَا بِهَاتَيْنِ السَّجْدَتَيْنِ وَإِنْ كَانَتْ رَابِعَةً فَالسَّجْدَتَانِ تَرْغِيمٌ لِلشَّيْطَانِ إِلَّا أَنَّ هِشَامًا بَلَغَ بِهِ أَبَا سَعِيدٍ الْخُدْرِيِّ هَكَذَا  

رَوَاهُ بَحْرُ بْنُ نَصْرٍ الْخَوْلَانِيُّ وَغَيْرُهُ عَنِ ابْنِ وَهْبٍ وَرَوَاهُ أَحْمَدُ بْنُ عَبْدِ الرَّحْمَنِ بْنِ وَهْبٍ عَنْ عَمِّهِ ابْنِ وَهْبٍ فَجَعَلَ الْوَصْلَ لِدَاوُدَ بْنِ قَيْسٍ 3802 أَخْبَرَنَاهُ مُحَمَّدُ بْنُ عَبْدِ اللهِ الْحَافِظُ ثنا أَبُو عَبْدِ اللهِ مُحَمَّدُ بْنُ يَعْقُوبَ حَدَّثَنِي أَبُو بَكْرِ بْنُ إِسْحَاقَ ثنا أَحْمَدُ بْنُ عَبْدِ الرَّحْمَنِ بْنِ وَهْبٍ ثنا عَمِّي قَالَ ثنا دَاوُدُ بْنُ قَيْسٍ عَنْ زَيْدِ بْنِ أَسْلَمَ عَنْ عَطَاءِ بْنِ يَسَارٍ عَنْ أَبِي سَعِيدٍ الْخُدْرِيِّ رَوَاهُ مُسْلِمٌ فِي الصَّحِيحِ عَنْ أَحْمَدَ بْنِ عَبْدِ الرَّحْمَنِ بْنِ وَهْبٍ وَرِوَايَةُ بَحْرِ بْنِ نَصْرٍ كَأَنَّهَا أَصَحُّ وَقَدْ وَصَلَ الْحَدِيثَ جَمَاعَةٌ عَنْ زَيْدِ بْنِ أَسْلَمَ مَعَ سُلَيْمَانَ بْنِ بِلَالٍ وَهِشَامِ بْنِ سَعْدٍ

See similar narrations below:

Collected by Mālik
malik:3-66Yaḥyá > Mālik > Zayd b. Aslam > ʿAṭāʾ b. Yasār

Yahya related to me from Malik from Zayd ibn Aslam from Ata ibn Yasar that the Messenger of Allah ﷺ said, "If you are uncertain in the prayer and do not know whether you have prayed three or four rakas, then pray a raka and make two prostrations from the sitting position before the taslim. If the raka that you prayed was the fifth, then you make it even by these two sajdas, and if it was the fourth, then the two prostrations spite Shaytan."  

مالك:٣-٦٦حَدَّثَنِي يَحْيَى عَنْ مَالِكٍ عَنْ زَيْدِ بْنِ أَسْلَمَ عَنْ عَطَاءِ بْنِ يَسَارٍ

أَنَّ رَسُولَ اللَّهِ ﷺ قَالَ إِذَا شَكَّ أَحَدُكُمْ فِي صَلاَتِهِ فَلَمْ يَدْرِ كَمْ صَلَّى أَثَلاَثًا أَمْ أَرْبَعًا فَلْيُصَلِّي رَكْعَةً وَلْيَسْجُدْ سَجْدَتَيْنِ وَهُوَ جَالِسٌ قَبْلَ التَّسْلِيمِ فَإِنْ كَانَتِ الرَّكْعَةُ الَّتِي صَلَّى خَامِسَةً شَفَعَهَا بِهَاتَيْنِ السَّجْدَتَيْنِ وَإِنْ كَانَتْ رَابِعَةً فَالسَّجْدَتَانِ تَرْغِيمٌ لِلشَّيْطَانِ