Is what the questioner did correct or incorrect: realizing the third rak'ah in Maghrib prayer with the intention of Maghrib, then completing the third rak'ah, then making the taslim, then praying Isha with a brother who was completing his prayer?
You must offer sincere repentance for delaying the Maghrib prayer past its time, as prayer is prescribed for specific times. It would have been better for you to pray Maghrib individually and then join the Imam, thereby avoiding the dispute concerning the invalidity of prayer due to differing intentions with the Imam.
The preponderant opinion is that it is permissible for the intention of the Imam and the follower to differ, and praying a fard (obligatory) prayer behind someone who is praying a shorter or longer fard prayer is valid. Praying Maghrib behind an Imam who is praying Isha is valid. And the prayer of a follower behind a latecomer who is completing the Isha prayer is permissible.
Summarized from the full answer at Ftawy · reviewed Sep 2, 2026
Read the full answer on Ftawyhttps://ftawy.com/en/questions/98505