It is human nature that many people tend to think about several things in namaz. Sometimes it happens that a person forgets in which rakat he was, and he is not sure about that? Should he go for SAJDE-SAHOO or should he repeat namaz another time?
One should endervour to develop complete concentration in Salaat. This may be achieved thoroughly regularly offering Nafl Salaat with prolonged Rakaats. If one is in Salaat and one falls into doubt as to what Rakaat one is in, one should apply prudent judgment and practise on that which one’s mind is inclined to. If, after applying this principle, one still remains in doubt, then one should practise on the lower of the two, e.g. if one is in doubt as to whether one is in the second or third Rakaat, then consider it to be the second Rakaat and add 2 more Rakaats to complete a four Rakaat Salaat. However, in this case, i.e. when acting to the lesser number, one should sit for Tashahhud after each Rakaat which could possibly be his last Rakaat. Therefore, in the above example, when one is confused about 2 or 3 Rakaats, and takes it to be his second Rakaat, he should sit after his next Rakaat and should recite Tashahhud, because there is a possibility that it is his fourth and last Rakaat. Then after reciting Tashahhud, he should stand up and complete the fourth Rakaat according to his presumption and should perform a Sajda-e-Sahw in the end. The above principles should be applied if this confusion occurs frequently. If this confusion occurs for the first time, the Salaat should be repeated. (Shaami vol.2 pg.92; HM Saeed)
and Allah Ta’ala Knows Best
Mufti Ebrahim Desai