Praise be to Allaah.
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 Qurâaan 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 Baaz 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 Fataawa wa Maqaalaat Mutanawwiâiah liâl-Shaykh âAbd al-âAzeez ibn Baaz (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 Allaah 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-Bukhaari, 1090; Muslim, 685.
And Anas (may Allaah be pleased with him) said: âWe went out with the Prophet (peace and blessings of Allaah be upon him) from Madeenah to Makkah, and we prayed two rakâahs each time, until we came back to Madeenah. Narrated by al-Bukhaari, 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-Bukhaari, 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 Allaah 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 Allaah has enjoined that even in the case of fighting. Allaah 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â¦â
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â Fataawa 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 Allaah 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 Allaah 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â Fataawa wa Rasaaâil al-Shaykh Ibn âUthaymeen, 15/422.