Answered by Shaykh Muhammad ibn Adam al-Kawthari
Sayyiduna Abu al-Shatha (Allah be pleased with him) narrates that: We were sitting in the Masjid with Sayyiduna Abu Hurayra (Allah be pleased with him) and the Muadhin made the call for prayer (adhan). A man stood up and started to walk out of the Masjid. Abu Hurayra (Allah be pleased with him) followed him with his sight until he left the Masjid. Then, Abu Hurayra (Allah be pleased with him) said: As for this person, he has indeed disobeyed Abul Qasim (the Messenger of Allah, Allah bless him & give him peace). (Sahih Muslim, no. 655, Sunan Tirmidhi and others)
Sayyiduna Abu Hurayra (Allah be pleased with him) narrates that the Messenger of Allah (Allah bless him & give him peace) ordered us that: If you are in the Masjid and the Adhan is pronounced, then one of you should not leave until he has prayed. (Musnad of Imam Ahmad)
Sayyiduna Uthman ibn Affan (Allah be pleased with him) narrates that the Messenger of Allah (Allah bless him & give him peace) said: Whoever is in the Masjid and the call for prayer (adhan) is made, then he leaves, he does not leave for a need neither does he intend to return, then this person is a hypocrite. (Sunan Ibn Majah, no. 734)
Due to the above narrations, the jurists (fuqaha) agree on the fact that it is prohibitively disliked (makruh tahriman) to leave the Masjid without an excuse after the call for prayer (adhan) is made.
However, if there is a genuine excuse or need, it will not be disliked rather perfectly permissible. Similarly, if one intends to return back to join the prayer in congregation, it will not be disliked.
The second Hadith of Sunan Ibn Majah clearly mentions that if one leaves the Masjid after Adhan without:
a) a need or excuse (hajah),
b) an intention of returning,
then, such a person will be considered a hypocrite (munafiq). Thus, if one left due to a need, such as reliving oneself, performing wudhu, etc or intended to return and join the congregation, then this will not be considered to be blameworthy.
The great Hanafi jurist (faqih), Imam al-Haskafi (Allah have mercy on him) states:
It is prohibitively disliked (makruh tahriman) due to the prohibition (m, mentioned in the Hadith) for an individual who has not yet performed his Salat to leave a Masjid in which the call to prayer (adhan) has been pronounced except for a person who is responsible for another congregation (m, in another masjid) or one leaves to pray in ones local Masjid and prayer (salat) has not been offered there or one leaves for the Masjid of his teacher in order to listen to his lesson or discourse or one leaves due to a need and intends to return. Similarly, it will not be disliked to leave for a person who has performed his Zuhr or Eisha prayer on his own once, but this will be disliked due to not performing Salat in congregation. However, if the Iqamah for prayer begins, then it will be disliked to leave, thus one should join the congregation with the intention of offering optional (nafl) prayer. Also, it will not be disliked to leave for the one who has performed his Fajr, Asr and Maghrib prayers once even if Iqamah begins, for it is disliked (makruh) to offer optional (nafl) prayers after the first two (m, i.e. Fajr and Asr) and in Maghrib due to the fact that it is prohibited to either offer Nafl prayers in odd number of Rakats (m, meaning one cannot offer three Rakat optional prayers) or due to going against the Imam (m, meaning that even if one added a fourth Rakat, it will not be permitted, for one is going against the Imam). (Durr al-Mukhtar)
The commentator of Durr al-Mukhtar, Allamah Ibn Abidin (Allah have mercy on him) has added some explanatory notes on the above text in his super commentary (hashiya). Here are some of the things he has mentioned:
a) The ruling of not leaving the Masjid will apply whether the Adhan is pronounced when one is in the Masjid or it was pronounced prior to one entering the Masjid meaning one enters the Masjid after the Adhan has been pronounced.
b) If one is responsible for another congregation (jamaah) in another Masjid, then it will not be disliked to leave, such as a Imam or Muadhin of another Masjid.
c) The author of al-Bahr al-Raiq (Imam Ibn Nujaym) has stated that one should not leave in order to go and perform Salat in ones local Masjid, because to perform Salat in ones local Masjid is recommended (mandub) and to leave the Masjid in which Adhan has been pronounced is prohibitively disliked.
d) If the congregation of prayer has already been taken place in ones local Masjid, then most definitely one should not leave.
e) If one offers his own Salat before the actual congregation, it will not be Makruh to leave the Masjid. However, this will be blameworthy due to not offering the prayer with the congregation (jamaah). In Zuhr and Eisha prayers, it will be Makruh to leave once the Iqmah has commenced. (See: Radd al-Muhtar ala al-Durr al-Mukhtar, matlab fi karahat al-khuruj min al-masjid bad al-adhan, 2/54-55)
In conclusion, it is prohibitively disliked (makruh tahriman) to leave the Masjid after the call to prayer (adhan) has been pronounced without any reason and without intending to return. However, if one intends to return or leaves due to some reason, such as one is an Imam in another Masjid, etc, it would be permissible. It is better not to leave merely in order to go and perform Salat in ones local Masjid.
As far your first question on hearing the Adhan whilst outside the Masjid is concerned, the jurists (fuqaha) have stated that upon listening to the Adhan, one should pause for a moment, listen to the Adhan and then carry on walking.
Allama Ibn Abidin (Allah have mercy on him) states:
If one hears the Adhan whilst walking, it is better to stand and respond to the Adhan (m, by repeating the words) so that one remains in one place (m, whilst responding to it). (Radd al-Muhtar, Bab al-Adhan, 1/397)
Also, when one hears the Adhan, it is necessary to respond to it by walking to the Masjid for prayer. Thus, if one was outside the Masjid and heard the Adhan, one should enter it and perform his prayer. However, if one expects to obtain the congregation (jamaah) elsewhere or there is some other reason, it will not be blameworthy to not have entered the Masjid instantly.
Allama Ibn Abidin (Allah have mercy on him) explains:
Responding to the Adhan by walking (bil qadam) is necessary (wajib) if not responding entails missing the congregation (jamaah). However, if one is able to obtain another congregation it will not be necessary (wajib). (Radd al-Muhtar, 1/399)
And Allah knows best.