All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His slave and Messenger.
The dominant opinion held by the Muslim jurists is that it is not a condition that the intention of the Imam and those whom he leads in Prayer be unified. Thus, he who performs an obligatory Prayer may join an Imam performing a supererogatory Prayer, and vice versa.
He who intends to perform Maghrib Prayer may join an Imam praying ‘Ishaa’. It was reported that Mu'ath ibn Jabal used to pray ‘Ishaa’ behind the Prophet and then return to his people to lead them in ‘Ishaa’ prayer. [Al-Bukhari and Muslim]
The Prophet approved his action, which entails that his intention is definitely different from that of those who performed the Prayer behind him, for he prayed supererogatory Prayers while they prayed obligatory ones.
On this Hadeeth, Imam ash-Shaafi'i based his ruling that he who intends to perform a full-length Prayer may join an Imam who is praying a shortened Prayer (being a traveler for example). In comparison, he who intends to perform Maghrib Prayer may join an Imam praying ‘Ishaa’ and when the latter stands up for the fourth Rak'ah (unit), the former says Taslim (ends his prayer- thus performing three Rak'ahs).
Allah knows best.