Sayyiduna Abu Sa'id Khudri (may Allah be pleased with him) said: The Messenger of Allah (peace and blessings be upon him) said: "When any one of you does not know whether he has prayed three rak'ahs or four, he should stand up and pray one more rak'ah, then perform two prostrations of forgetfulness (sujood as-sahw). If that rak'ah turns out to be the fifth, then these prostrations will make his prayer even; and if it is the fourth rak'ah, then these two prostrations will humiliate Satan." Imam Darimi (may Allah have mercy on him) said: I am one who holds this view and acts upon it.
Hadith Referenceسنن دارمي / من كتاب الصللاة / 1534
(Commentary on Hadiths 1532 to 1534) From here, Imam Darimi rahimahullah has begun the discussion of the prostration of forgetfulness (sujud al-sahw). In the first hadith, it is mentioned that Shaytan casts whispers, and the one praying forgets how many units (rak‘at) he has performed. He should act upon what he is certain of; that is, if he is certain about having prayed four rak‘at, then he should perform the prostration of forgetfulness (sujud al-sahw). And if he is in doubt, unable to be certain whether he has prayed three or four rak‘at, then in such a case, he should perform one more rak‘ah, then perform the prostration of forgetfulness. Now, the issue arises whether the prostrations should be performed before the salutation (salam) or after it. There is evidence for both ways, the details of which, insha’Allah, are coming ahead.