May Allah (Subhanahu wa Ta'ala) reward you all.
I was praying Asr and I was confused whether I was in the second rakah or the third (during when I was deciding whether I should sit for the tashahhud or not). I thought that I was most likely in the second rakah, so I sat and started to recite tashahhud. Whilst reciting tashahhud, however, I remembered that I actually have already recited the tashahhud and hence, I am actually in the third rakah. Thus, I stood up and continued my prayer. I did prostration for forgetfulness after saying salam. Did I do the right thing? Thank you.
بِسْمِ اللهِ الرَّحْمنِ الرَّحِيْم
In the name of Allah, the Most Gracious, the Most Merciful
If this is the first time in one's life this has happened, then the prayer should be repeated completely. However, if this has happened on more than one occasion, then there is no need to repeat the prayer.
If you are capable of making a strong assumption regarding how many rakat you have prayed so far, then it will suffice to pray according to this assumption and then perform sajdah of forgetfulness. However, if one cannot decide whether 2 or 3 rakat were performed (for example), then the lesser should be acted upon, in this case, 2 rakats.
In the case mentioned above, you had acted according to the dominant assumption originally, then realized what was correct and acted accordingly by performing the prostration of forgetfulness. Thence, the prayer will be valid and there is no need to repeat, inshaAllah.
Contemporary Fatwas (Mufti Taqi Uthmani) Pg 11
Only Allah knows best
Written by Maulana Nabil Khan
Checked and approved by Mufti Mohammed Tosir Miah
Darul Ifta Birmingham