It is narrated from Sayyiduna Ibn Mas'ud (may Allah be pleased with him) that he said: If, while sitting in prayer, you become doubtful whether you have prayed three rak'ahs or four, then if your predominant thought is that you have prayed three rak'ahs, stand up and pray one rak'ah, then say salam and perform two prostrations of forgetfulness (sahw), and recite tashahhud and then say salam. But if your predominant thought is that you have prayed four rak'ahs, then say salam, then perform two prostrations of forgetfulness, then recite tashahhud and say salam.
Hadith Referenceمسند احمد / مسند المكثرين من الصحابة / 4076