When you doubt how many rak'ahs

In doubt, build on what you are sure of and make two prostrations of forgetfulness.

Narrated by Abu Sa'id al-Khudri

When one of you is in doubt in his prayer and does not know how much he has prayed, three or four, let him cast aside the doubt and build on what he is certain of, then prostrate twice before the salam.

Sahih Muslim 571

What this hadith teaches

Taking the lower number is the certain one. These two extra prostrations (sujud as-sahw) also humiliate Shaytan who caused the doubt.

Reference and grade

Grade
Sahih (in Sahih Muslim)
Narrator
Abu Sa'id al-Khudri

The hadith in Arabic نص الحديث

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

Arabic text of Sahih Muslim 571, as given on sunnah.com.