I heard the Prophet ﷺ say, "When one of you becomes uncertain during his prayer and does not know whether he has prayed one rakah or two, let him assume it was one. If he does not know whether he has prayed two or three, let him assume it was two. If he does not know whether he has prayed three or four, let him assume it was three. Then let him perform two prostrations before giving the salam."
حدثنا محمد بن بشار حدثنا محمد بن خالد ابن عثمة البصري حدثنا إبراهيم بن سعد قال حدثني محمد بن إسحق عن مكحول عن كريب عن ابن عباس
سمعت النبي ﷺ يقول إذا سها أحدكم في صلاته فلم يدر واحدة صلى أو ثنتين فليبن على واحدة فإن لم يدر ثنتين صلى أو ثلاثا فليبن على ثنتين فإن لم يدر ثلاثا صلى أو أربعا فليبن على ثلاث وليسجد سجدتين قبل أن يسلم
[Hasan] Ibn Majah also narrated it in Iqamat al-Salawat, chapter on what has been said about one who has doubt in his prayer and returns to certainty, no. 1209, from the hadith of Muhammad bin Ishaq bin Yasar with the same wording, and he stated explicitly that he had heard it directly, as found in Abu Yaala, no. 839. Hakim graded it sahih according to the criterion of Muslim, 1/324-325, and Dhahabi agreed with him.