There are two issues connected to this question.
1 – Should the one who joined the prayer late do the prostration of forgetfulness if he forgets something in his prayer?
Al-Bahooti said in Sharh Muntaha al-Iraadaat (1/232):
He (i.e., the one who joined the prayer late) should also do the prostration of forgetfulness in the following situations:
-He should do the prostration of forgetfulness with regad to the part of the prayer that he did with his imam, even if he left the imam after that f or a reason.
-The one who joined the prayer late should also do the prostration of forgetfulness if he forgets something in the part of the prayer that he offers alone, which is what he makes up after the imam has said the salaam, even if he prostrated with him because of his (the imam’s) mistake, because now he is praying alone. End quote.
Shaykh Ibn Baaz (may Allaah have mercy on him) said:
As for the one who joined the prayer late, he should do the prostration of forgetfulness if he made a mistake with his imam, or if he made a mistake in the part of the prayer that he offers alone after the imam has finished it. End quote.
Fataawa Ibn Baaz, 11/268.
It says in Fataawa al-Lajnah al-Daa’imah (7/151):
If a person forgets something whilst making up the part of the prayer that he missed, or he is not sure about his prayer, then he should act on the basis of what is certain – which is the lower number – and complete his prayer, then do the prostration of forgetfulness. End quote.
2 – When should the prostration of forgetfulness be done – before the salaam or after?
The Sunnah is that if a person forgets the first tashahhud, he should do the prostration of forgetfulness before the tasleem.
Al-Bukhaari (1224) and Muslim (570) narrated that ‘Abd-Allaah ibn Buhaynah said: The Messenger of Allaah (peace and blessings of Allaah be upon him) led us in praying two rak'ahs of prayer, then he stood up and did not sit, and the people stood up with him. When he had finished praying and we were waiting for his tasleem, he said takbeer and prostrated twice whilst he was sitting, before the tasleem, then he said the tasleem.
This hadeeth indicates that the prayer of one who forgets the first tashahhud is valid, and that he should do the prostration of forgetfulness before saying the tasleem at the end of the prayer.
In the answer to question no. 12527 we have quoted the words of Shaykh ibn ‘Uthaymeen (may Allaah have mercy on him) who said:
Thus it becomes clear that this should be done before the salaam if you have omitted one of the obligatory parts of the prayer or if you are not sure how many rak’ahs you have done, and neither of the two choices seems more likely to be the case. And it should be done after the salaam if you have added something extra to the prayer or you are not certain but one of the two choices seems more likely to be the case.
See Majmoo’ Fataawa al-Shaykh, 14/14-16
Based on this, you did well when you did the prostration of forgetfulness before the salaam, and your prayer is valid in sha Allah.
And Allaah knows best.