Praise be to Allah.
When a person enters the Sacred Mosque, one of two things must be the case:
1 – He is entering with the intention of performing tawaaf, whether it is for Hajj or ‘Umrah or is a voluntary tawaaf.
In this case the first thing he should do is tawaaf, and it is not prescribed for him to start with the two rak’ahs for greeting the mosque before doing tawaaf, because the Messenger of Allaah (peace and blessings of Allaah be upon him) did not do that, and neither did any of his companions. This is the view of the majority of fuqaha’, and no one disagreed with that apart from a few individuals, such as Ibn ‘Aqeel among the Hanbalis, as was narrated from him by Ibn Taymiyah in Sharh ‘Umdat al-Fiqh.
An exception is made from that if there is some impediment such as severe crowding at the beginning of tawaaf. In that case he may pray two rak’ahs to greet the mosque, and wait until the crowding eases off so that he may begin tawaaf.
2 – He is entering with the intention of praying or sitting or attending a halaqah for knowledge or listening to a reminder, or to read Qur’aan, or some other act of worship.
It is mustahabb for him to pray two rak’ahs to greet the mosque, because of the general meaning of the hadeeth of Abu Qataadah (may Allaah be pleased with him), that the Prophet (peace and blessings of Allaah be upon him) said: “When one of you enters the mosque, let him not sit down until he has prayed two rak’ahs.” Narrated by al-Bukhaari (1167) and Muslim (714).
With regard to the hadeeth that some people quote, “Greeting the mosque is tawaaf,” it has no basis in the books of Sunnah and it was not narrated from the Prophet (peace and blessings of Allaah be upon him) with any isnaad at all, so it is not permissible to attribute it to him.
Shaykh al-Albaani said in Silsilat al-Ahaadeeth al-Da’eefah (no. 1012):
I do not know of any basis for it, although it is often quoted. It was narrated by the author of al-Hidaayah who was a Hanafi, with the wording: “Whoever comes to the House, let him greet it with tawaaf.” Al-Haafiz al-Zayla’i indicated in his commentary that it has no basis. He said (2/51): It is very strange (ghareeb jiddan). Al-Haafiz Ibn Hajar was clearer than that, as he said in al-Diraayah (p. 192): I did not find it.
I – i.e., Shaykh al-Albaani – say: I do not know of anything in the Sunnah, word or deed, that testifies to this. Rather the general meaning of the evidence about praying before sitting down in the mosque includes the Sacred Mosque too, and the view that its greeting is tawaaf is contrary to the general meaning referred to, so it should not be accepted unless it is proven. And there is no proof, especially since it is proven by experience that it is not possible for the one who enters the Sacred Mosque to do tawaaf every time he enters during the Hajj season. Praise be to Allaah Who has made the matter broad in scope. “and [Allaah] has not laid upon you in religion any hardship” [al-Hajj 22:78].
It should be noted that this ruling applies to people other than those in ihraam. The Sunnah for the pilgrim in ihraam is to start with tawaaf and pray the two rak’ahs after that. See: Bida’ al-Hajj wa’l-‘Umrah in my essay Manaasik al-Hajj wa’l-‘Umrah, no, 37. End quote.
Al-Hattaab al-Maaliki said in Mawaahib al-Jaleel Sharh Mukhtasar Khaleel (2/375):
Whoever enters the mosque in Makkah, the greeting of the Sacred Mosque in his case is circumambulation of the House (tawaaf). This applies to the newcomer who comes in ihraam. It is required of him when he enters the Sacred Mosque to start with tawaaf al-qudoom (tawaaf of arrival), if he has entered ihraam for Hajj or Qiraan; or with tawaaf of ‘umrah if he has entered ihraam for ‘umrah; or with tawaaf al-ifaadah if he has entered after coming back from ‘Arafah. He is not required to pray when he enters. The same applies for a person other than a newcomer, if he enters the Sacred Mosque with the intention of performing tawaaf when he enters: the greeting of the mosque in his case is tawaaf, and he is not required to pray (two rak’ahs to greet the mosque) in that case.
But for the one who is not a newcomer, when he enters the Sacred Mosque with the intention of praying in the mosque or seeing the Sacred House, but not of doing tawaaf, then he should pray two rak’ahs.
Ibn Rushd said: Tawaaf around the House is prayer, so when he enters (the mosque) intending to do tawaaf, he should start with tawaaf, and if he enters it not intending to do tawaaf at a time when naafil prayers may be offered, he should start by praying two rak’ahs. End quote.
It says in al-Mawsoo’ah al-Fiqhiyyah (10/306):
The majority of fuqaha’ are of the view that greeting the Sacred Mosque means tawaaf for the newcomer to Makkah, whether he is a merchant, a pilgrim or otherwise, because ‘Aa’ishah (may Allaah be pleased with her) said: When the Prophet (peace and blessings of Allaah be upon him) came to Makkah, he did wudoo’, then he circumambulated the House. And the two rak’ahs for greeting the mosque are replaced by the two rak’ahs following tawaaf.
As for the Makkan (resident of Makkah) who is not required to do tawaaf, and who did not enter the mosque in order to do tawaaf, rather he came to pray or read Qur’aan or to seek knowledge, the greeting of the Sacred Mosque in his case is prayer, as in any other mosque. End quote.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) was asked: Is the greeting of the Sacred Mosque praying two rak’ahs or doing tawaaf?
He replied: The Sacred Mosque is like any other mosque: the one who enters it to pray, listen to a reminder and so on should pray two rak’ahs, as in any other mosque, because of the general meaning of the words of the Prophet (peace and blessings of Allaah be upon him): “When one of you enters the mosque, let him not sit until he prays two rak’ahs.” Narrated by al-Bukhaari (1167). But if he enters the mosque to do tawaaf, such as a person who is doing ‘umrah and enters the mosque to do the tawaaf of ‘umrah, or to do a voluntary tawaaf, then in this case the tawaaf replaces the two rak’ahs for greeting the mosque, because when he does tawaaf he is going to pray two rak’ahs after tawaaf. End quote.
Majmoo’ Fataawa Ibn ‘Uthaymeen (22/286).
And Allaah knows best.