We were with the Messenger of Allah ﷺ one day when he asked, "Did you enter the mosque?"I said, "Yes."He asked, "Did you pray in it?"I said, "No."He said, "Then go and pray two rakahs."
كنا عند رسول الله ﷺ يوما فقال: أدخلت المسجد؟ قلت: نعم. فقال: أصليت فيه؟ قلت: لا. قال: فاذهب فاركع ركعتين.
No references are available.
Ibn Khuzayma (1828) narrated it from Rabi bin Sulayman, from Ibn Wahb, who said, Usama told me, from Muaz bin Abdullah bin Habib Juhani, who said, I heard Jabir bin Abdullah say (and he mentioned it). Its chain is hasan because of Usama, who is Ibn Zayd Laythi, and his teacher Muaz bin Abdullah, for the two of them make the hadith hasan.The hadiths of this chapter indicate the permissibility of the two rakahs for whoever enters the masjid before sitting down, and there is no disagreement about this. The disagreement occurred only regarding performing them during the disliked times.Shafii and some of the Hanbalis said, it is permissible to perform them during the disliked times. Malik and Abu Hanifa said, that is disliked. The reason for this is the existence of two conflicting general statements, the command to perform the two rakahs for everyone who enters, and the prohibition of praying during the disliked times.The proponents of the first view said, the hadiths prohibiting prayer during the disliked times are specific to prayers that have a reason, so the dislike remains for what has no reason, arguing from the hadith of Abu Qatada, because it is general, and the Prophet ﷺ did not exempt the disliked times. Likewise, every prayer that has a reason, such as the eclipse prayer, the funeral prayer, and performing the two rakahs of tawaf and others.Given the existence of disagreement among the scholars, the balanced approach is that whoever prays should not be reproached for it, and likewise whoever does not pray should not be commanded to do so.As for Masjid al-Haram, the greeting of the House is the tawaf for whoever wishes, then performing the two rakahs of tawaf. But if he does not perform tawaf and wishes to sit, then he must perform the greeting of the masjid.For more, see Minna Kubra (2/421).