This excerpt is taken from Sheikh al-Islam Imam Ibn Taymiyyah's book Al-Jawab al-Bahir fi Zuwar al-Maqabir, translated by Sheikh Ataullah Saqib.
The Companions Used to Go to Mosques, Not Graves, for Worship
The noble Companions, may Allah be pleased with them, used to turn only to mosques for prayer, supplication, remembrance, and recitations, which were built solely for the remembrance of Allah Almighty. They did not even attempt to go towards the graves of the Prophets and the righteous, which were prohibited from being made places of worship. The Companions, may Allah be pleased with them, continued to act in the same way as they did during the blessed life of the Mercy to the Worlds, peace and blessings be upon him.
Imam Malik’s Position and Authentication by Hadith
Islamic scholars, especially Imam Malik, considered it disliked to go near the honored grave when entering or leaving the Prophet's Mosque, even if the intention was only to send salutations and peace. Their school of thought is supported by the following arguments and evidences.
كان رسول الله صلى الله عليه وسلم يأتى قباء كل سبت راكبا وماشيا فيصلى فيه ركعتين
"The Messenger of Allah used to go to the Quba Mosque every week, sometimes on foot and sometimes riding, and there he would perform two rak'ahs of prayer."
Reference: (Sahih Bukhari, Book of the Virtue of Prayer in Mecca and Medina Mosque: Chapter on Visiting Quba Mosque on Foot and Riding, Hadith: 1194; Sahih Muslim, Book of Hajj: Chapter on the Virtue of Quba Mosque and Prayer Therein, Hadith: 1399)
This was also the regular practice of Ibn Umar, may Allah be pleased with them both.
The above authentic hadith confirms that the Prophet prayed the Friday prayer in the mosque and went to the Quba Mosque on Saturdays to perform two rak'ahs of prayer. Both the Quba Mosque and the Prophet's Mosque were founded on the basis of piety, as testified by the Lord Almighty Himself:
لَا تَقُمْ فِيهِ أَبَدًا ۚ لَّمَسْجِدٌ أُسِّسَ عَلَى التَّقْوَىٰ مِنْ أَوَّلِ يَوْمٍ أَحَقُّ أَن تَقُومَ فِيهِ ۚ فِيهِ رِجَالٌ يُحِبُّونَ أَن يَتَطَهَّرُوا ۚ وَاللَّهُ يُحِبُّ الْمُطَّهِّرِينَ ﴿١٠٨﴾
"The mosque founded on piety from the first day is more worthy of your standing in it (for prayer). In it are men who love to purify themselves; and Allah loves those who purify themselves."
Reference: (9-At-Tawbah:108)
In the books of hadith, it is narrated in various ways that the Prophet, peace be upon him, asked the people of Quba what deed they performed for which Allah praised them. They replied that they used water for purification (istinja). According to the narration in Sunan Abi Dawood, the Prophet, peace be upon him, said:
نزلت هذه الآية فى مسجد اهل قباء فيه رجال يحبون أن يتطهروا قال كانوا يستنجون بالماء فنزلت فيهم هذه الآية
"This verse was revealed concerning the people of Quba, that in it are men who love to purify themselves." The narrator says that the people of Quba used water for purification. This verse was revealed about them."
Reference: (Sunan Abi Dawood, Book of Purification: Chapter on Istinja with Water, Hadith: 44; Sunan Tirmidhi, Book of Tafsir: Chapter on Surah At-Tawbah, Hadith: 3100; Sunan Ibn Majah, Book of Purification: Chapter on Istinja with Water, Hadith: 357)
In the Sahihayn, it is narrated from Sa'd, may Allah be pleased with him:
أنه سأل النبى صلى الله عليه وسلم عن المسجد الذى أسس على التقوى وهو فى بيت بعض أزواجه فأخذ كفا من حصى فضرب بالأرض ثم قال هو مسجدكم هذا لمسجد المدينة
"They asked the Messenger of Allah about the mosque which was founded on piety while he was in one of his houses. He picked up a handful of pebbles, threw them on the ground, and said: This is your mosque." Meaning the Mosque of Medina."
Reference: (Sahih Muslim, Book of Hajj, Chapter on the Mosque Founded on Piety, Hadith: 1398; narrated from Abu Sa'id Al-Khudri and not found in Sahihayn from Sa'd, and Allah knows best!)
These narrations prove that the foundations of both mosques were based on piety. However, the Prophet's Mosque is more deserving of this title, and the mentioned verse was revealed concerning the Quba Mosque. This is because the hypocrites built the Mosque of Dhirar next to the Quba Mosque, where the Prophet, peace be upon him, was prevented by Allah from praying.
The meaning is that the Prophet used to go to the Quba Mosque every week, a practice that Ibn Umar, may Allah be pleased with them both, adopted as his routine. Also remember that Ibn Umar, may Allah be pleased with them both, did not go to the honored grave every day or every week while he was in Medina. However, whenever he returned from travel, he would go to the honored grave to offer salutations. Similarly, many companions, may Allah be pleased with them, when returning from travel, did not come to the honored grave, neither for salutations nor for supplications, nor was it their habit to stand outside the blessed chamber or try to enter it as Ibn Umar, may Allah be pleased with them both, used to do. If they wanted to ask Umm al-Mu'minin Aisha, may Allah be pleased with her, a question, they would go inside the blessed chamber. On that occasion, they would greet the Prophet, peace be upon him, as they used to during his lifetime.