Praise be to Allah.
The scholars differed concerning the ruling on offering an obligatory prayer behind an imam who is offering a naafil prayer. We have quoted some of the scholarly opinions on this matter in the answer to question number 79136.
Imam al-Shafaa’i and Ibn al-Mundhir are of the view that this is permissible, and this is also narrated from Ahmad. The scholars of the Standing Committee and Shaykh Ibn Baaz also favoured this view, and in the question mentioned above we quoted him as saying that it is permissible, namely praying ‘Isha’ behind one who is praying Taraweeh, and that the person praying behind him should complete his prayer on his own after the imam says the salaam.
As for what the questioner did, sitting after the imam said the salaam so that he could follow him in the next two rak’ahs of Taraweeh, there are two scholarly opinions concerning the one who starts his prayer on his own -- can he follow an imam who is leading a congregation? Some of them said that this is not allowed and some of them said that it is valid.
Shaykh al-‘Uthaymeen did not give a definitive opinion concerning this action and he said, after stating that it is permissible for one who is offering an obligatory prayer to pray behind one who is offering a naafil prayer, which includes it being permissible to offer ‘Isha’ prayer behind one who is praying Taraweeh:
Rather what I am not sure about is their waiting for the imam until he starts the next two rak’ahs and completing the prayer with him. This is what I am not sure about, because the Messenger (blessings and peace of Allah be upon him) said: “Whatever you catch up with, pray, and whatever you miss, complete it.” The apparent meaning is that a person should complete what he missed with his imam on his own, meaning that he should not wait until the imam starts another prayer. Rather we say: When the imam says the salaam at the end of the prayer in which you caught up with him, then complete the prayer and do not wait until he starts another prayer. End quote.
Fataawa Noor ‘ala al-Darb (Tape no. 15, side B)
Al-Nawawi thought it more likely that this is permissible, as he said:
If he prays ‘Isha’ behind Taraweeh, that is permissible, and when the imam says the salaam, he should stand up to do the remaining two rak’ahs. It is better for him to complete them on his own. If the imam gets up to do two more rak’ahs of Taraweeh, he should form the intention to follow him again in his two rak’ahs. There are two opinions as to whether it is permissible for one who started the prayer on his own to then intend to follow the imam. The more correct view is that it is valid. End quote.
Al-Majmoo’ Sharh al-Muhadhdhab, 4/270
Based on this, the prayer was valid and you do not have to repeat it, but it is better -- in the future -- to complete your prayer on your own and not to join with the imam again.
The brother who asked this question does not have to repeat the prayer or to repeat the two rak’ahs. The ‘Isha’ prayer that he offered behind the imam who was praying Taraweeh is valid. But we think that it is better for him to complete what remains for him of his prayer on his own. But if he follows the last two rak’ahs with the imam again, that is permissible.
And Allah knows best.