Praise be to Allah.
Prayer in congregation is obligatory and it is not permissible for a Muslim not to do that unless he has an excuse. We have already quoted the evidence for that from the Quran and Sunnah. Please see question no. 8918.
Based on this, you have to offer the prayers in congregation in the mosque. If the imam is a resident (is not travelling) then you should offer the prayer in full with him, and not shorten it.
Shaykh Ibn Baz was asked: If a person travels to Jeddah, for example, is he allowed to shorten his prayers or does he have to pray with the congregation in the mosque?
If the traveller is still en route, it does not matter, but if he has reached his destination then he should not pray on his own, rather he has to pray with the people and offer the prayer in full. But if he is still on the road and is alone and the time for prayer comes, there is nothing wrong with him praying on his own and shortening the prayers whilst travelling, making the four-rakah prayers two rak’ahs.
Majmoo Fatawa wa Maqaalaat Mutanawwi’iah li’l-Shaykh ‘Abd al-‘Azeez ibn Baz (12/297).
Shaykh Ibn ‘Uthaymeen was asked: when and how should a traveller pray?
The traveller may pray two rak’ahs from when he leaves his city or town, until he returns to it, because ‘Aa’ishah (may Allah be pleased with her) said: “When the prayer was first enjoined it was two rak’ahs, then the prayer of the traveller remained like that but the prayer of one who is not travelling was increased to four.” Narrated by al-Bukhari, 1090; Muslim, 685.
And Anas (may Allah be pleased with him) said: “We went out with the Prophet (peace and blessings of Allah be upon him) from Madeenah to Makkah, and we prayed two rak’ahs each time, until we came back to Madeenah. Narrated by al-Bukhari, 1081; Muslim, 693.
But if a traveller prays with an imam he should pray four rak’ahs, whether he catches up with the prayer from the beginning or he misses part of it, because the general meaning of the Prophet’s words: “When you hear the iqaamah, then walk to the prayer, and you should be tranquil and dignified, and not rush. Whatever you catch up with, pray, and whatever you miss, complete it.” Narrated by al-Bukhari, 636; Muslim, 602. The general meaning of the phrase “Whatever you catch up with, pray, and whatever you miss, complete it” includes travellers who pray behind an imam who is offering a four-rak’ah prayer, and others.
Ibn ‘Abbaas (may Allah be pleased with him) was asked why a traveller should pray two-rak’ahs when he is alone and four when he prays behind a resident. He said: “That is the Sunnah.” Narrated by Muslim, 688; Ahmad, 1865.
The obligation to pray in congregation is not waived for the traveller, because Allah has enjoined that even in the case of fighting. Allah says (interpretation of the meaning):
“When you (O Messenger Muhammad) are among them, and lead them in As-Salaah (the prayer), let one party of them stand up [in Salaah (prayer)] with you taking their arms with them; when they finish their prostrations, let them take their positions in the rear and let the other party come up which have not yet prayed, and let them pray with you…” [al-Nisa’ 4:102]
Based on this, if the traveller is in a city or town other than his own, he has to attend prayers in congregation in the mosque if he hears the call to prayer, unless he is far away or fears that he may miss meeting up with his travelling companions, because of the general meaning of the evidence which indicates that the one who hears the adhaan or iqaamah is obliged to pray in congregation.
Majmoo’ Fatawa wa Rasaa’il al-Shaykh Ibn ‘Uthaymeen, 15/252.
He was also asked:
If I am travelling and I hear the call to prayer, do I have to pray in the mosque? If I pray in the place where I am staying, is there anything wrong with that? If the duration of my trip is more than four consecutive days, should I shorten my prayers or offer them in full?
He replied: If you hear the adhaan when you are in the place where you are staying, then you have to attend the mosque, because the Prophet (peace and blessings of Allah be upon him) said to a man who asked him for permission not to pray in congregation: “Can you hear the call?” He said, “Yes.” He said: “Then answer it.” Narrated by Muslim, 6533. And he (peace and blessings of Allah be upon him) said: “Whoever hears the call and does not come, there is no prayer for him (i.e., his prayer is not valid), except for one who has an excuse.” Narrated by al-Tirmidhi, 217; classed as saheeh by al-Albaani in Saheeh al-Tirmidhi.
There is no evidence to indicate that this ruling applies only to the traveller, unless your going to the mosque will cause you some problems in your journey, such as if you need to rest and sleep and you want to pray in the place where you are staying so that you can sleep, or you are afraid that if you go to the mosque the imam will delay the prayer, and you want to leave and you are scared that you may miss the train or plane, etc.
Majmoo’ Fatawa wa Rasaa’il al-Shaykh Ibn ‘Uthaymeen, 15/422.