A man was travelling and reached the city to which he was travelling. He wanted to pray with an imam who is resident in that city. Is his prayer valid?
There is no reason why a traveller should not pray behind a resident, regardless of whether the way of praying differs or not. The difference in intentions does not have any effect, but if the traveller catches up with the imam in less than a rak’ah, he should pray the prayer in shortened form.
For example: the traveller catches up with the imam in the final tashahhud of the Zuhr prayer. In that case he should pray Zuhr as two rak'ahs. This is how he should pray if he did not catch up with one rak’ah or more.
But if he catches up with the resident in one rak’ah or more, then he has to pray the prayer in full (i.e., four rak’ahs). This is the view of most of the scholars and it was narrated from Ibn ‘Abbaas and Ibn ‘Umar (may Allaah be pleased with them both).
In Saheeh Muslim (688) it was narrated that Musa ibn Salamah al-Hadhali said: “I asked Ibn ‘Abbaas, ‘How should I pray if I am in Makkah and I do not pray with the imam?’ He said, ‘Two rak’ahs, and this is the way of Abu’l-Qaasim (peace and blessings of Allaah be upon him).”
In Saheeh Muslim (694) it is narrated that Naafi’ said: “When Ibn ‘Umar was with the imam he would pray four rak’ahs and if he prayed alone he would pray two rak’ahs.”
Al-Bayhaqi narrated in al-Sunan via Sulaymaan al-Tameemi that Abu Majlaz said: “I said to Ibn ‘Umar, ‘If a traveller catches up with the prayer of the people – i.e., the local residents – should he pray two rak’ahs or pray as they are praying?’ He laughed and said, ‘He should pray as they are praying.’”
If the form of the prayer is different, e.g. if the traveller is praying Maghrib behind a resident who is praying ‘Isha’, then in that case he should sit after the third rak’ah.
Should he complete his prayer and leave, or should he wait for the imam to complete his prayer and say the tashahhud and salaam with him?
There is some evidence to support both options, but the more correct in my view is the second, which is to wait.
If it is the other way round, and the traveller is praying ‘Isha’ behind a resident who is praying Maghrib, then it seems to me that in this case he should pray two rak’ahs and then sit until the imam has completed his prayer, then say the tashahhud and salaam with him.
And Allaah knows best.