Praise be to Allah.
Your praying ‘Isha’ behind someone who was praying Taraweeh or Witr is valid according to the more correct of the two scholarly opinions. This issue is known to the fuqaha’ as the prayer of one who is offering an obligatory prayer behind one who is offering a naafil prayer. Ibn Qudaamah (may Allaah have mercy on him) said in al-Mughni (2/30): There are two reports concerning the prayer of one who is offering an obligatory prayer behind one who is offering a naafil prayer, one of which is that it is not valid. This view was favoured by most of our companions, and this is the view of al-Zuhri, Maalik, and ashaab al-ra’y, because the Prophet (peace and blessings of Allaah be upon him) said: “The imam is appointed to be followed, so do not differ from him.” Agreed upon.
The other view is that it is permissible. This is the view of al-Shaafa’i and Ibn al-Mundhir. This is more correct, because of the report narrated by Jaabir ibn ‘Abd-Allaah that Mu’aadh would pray with the Messenger of Allaah (peace and blessings of Allaah be upon him), then go back and lead his people in that prayer. Agreed upon. And it is narrated that the Prophet (peace and blessings of Allaah be upon him) led a group of his companions in the fear prayer, and did two rak’ahs then said the salaam, then he led the other group in two rak’ahs then said the salaam. Narrated by Abu Dawood. The second two rak’ahs were naafil for him, but he was leading people who were offering the obligatory prayer.
As for the hadeeth that they quote, what it means is: Do not differ from the imam in his actions, based on the fact that he said: “When he bows, then bow, and when he rises from bowing then rise from bowing, and when he prostrates then prostrate, and if he prays sitting down then you should all pray sitting down.” End quote.
The scholars of the Standing Committee for Issuing Fatwas was asked: What should be done if a person comes after ‘Isha’ prayer and the prayer has ended, and the imam gets up to pray Taraweeh. Should he follow the imam with the intention of praying ‘Isha’, or should he get up and pray by himself or with a jamaa’ah (congregation) if there is one?
They replied: it is permissible for him to pray ‘Isha’ in congregation with one who is praying Taraweeh, then when the imam says the salaam after two rak’ahs, the one who is praying ‘Isha’ behind him should stand up and pray two more rak’ahs in order to complete ‘Isha’ prayer. End quote.
Fataawa al-Lajnah al-Daa’imah (7/402).
Shaykh Ibn Baaz (may Allaah have mercy on him) was asked: If a Muslim comes to the mosque and finds the congregation praying Taraweeh, and he has not prayed ‘Isha’, should he pray with them with the intention of praying ‘Isha’?
He replied: There is nothing wrong with him praying with them with the intention of praying ‘Isha’, according to the more correct of the two scholarly opinions. When the imam says the salaam, he should stand up and complete his prayer. End quote from Majmoo’ Fataawa al-Shaykh Ibn Baaz (12/181).
The rak’ah that you prayed with the imam will not be counted as Witr for two reasons:
1 – You started to pray with the intention of praying ‘Isha’, so it is a rak’ah that is counted as part of ‘Isha’, and you have to complete the rest of the prayer after the imam says the salaam.
2 – Witr cannot be valid unless one has finished ‘Isha’, because of the report narrated by Imam Ahmad (23339), that the Prophet (peace and blessings of Allaah be upon him) said: “Allaah has added a prayer for you, and it is Witr, so pray it between ‘Isha’ and Fajr.”
Classed as saheeh by al-Albaani in Silsilat al-Ahaadeeth al-Saheehah 9108).
To sum up: What you did is valid and we ask Allaah to accept it from you.
And Allaah knows best.