Hadith on Prayer: What to do if you are unsure how much you prayed?

Abu Sa’eed reported: The Messenger of Allah, peace and blessings be upon him, said, “If one of you doubts his prayer and does not know how much he has prayed, whether three or four cycles (raka’at), then let him put aside his doubt and act upon what he is certain of; then, perform two prostrations before he offers salutations of peace. If he had (mistakenly) prayed five cycles, the prostrations will make his prayer even. If he had prayed precisely four cycles, it will be humiliation for Satan.”

Source: Ṣaḥīḥ Muslim 571

An-Nawawi said, “Malik, Ash-Shafi’ee, and Ahmad, may Allah be pleased with them, as well as the majority said that when one has doubt in his prayer, whether he prayed three or four cycles, one is required to act based upon what is known with certainty. Then, one completes the fourth cycle and performs the prostrations of forgetfulness.”

Source: Sharḥ al-Nawawī ‘alá Ṣaḥīḥ Muslim 389

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

571 صحيح مسلم كتاب المساجد ومواضع الصلاة باب السهو في الصلاة والسجود له

قال النووي وَقَالَ مَالِكٌ وَالشَّافِعِيُّ وَأَحْمَدُ رَضِيَ اللَّهُ عَنْهُمْ وَالْجُمْهُورُ مَتَى شَكَّ فِي صَلَاتِهِ هَلْ صَلَّى ثَلَاثًا أَمْ أَرْبَعًا مَثَلًا لَزِمَهُ الْبِنَاءُ عَلَى الْيَقِينِ فَيَجِبُ أَنْ يَأْتِيَ بِرَابِعَةٍ وَيَسْجُدَ لِلسَّهْوِ

389 شرح النووي على مسلم