Visiting the Mosque of the Prophet ﷺ is not part of the conditions of Hajj, and neither is it one of its pillars or obligations; rather it is a recommended act (Sunnah) and can be performed at any time.Furthermore, it is compulsory that the intention for the visit should be to pray in the Mosque, and not to visit the grave. On the authority of Abu Hurairah t, who said that the Prophet ﷺsaid:
“No one should journey to any Mosque except for three Mosques: the Haram Mosque, the Prophet’s Mosque and al-Aqsaa Mosque.”
[ Source: Muslim.]
Shayk al-Islam Ibn Taymiyyah said: “If the intention of traveling is to visit the grave of the Prophet ﷺ , but not to pray therein, then the view of the Imams and majority of scholars is that it is neither a legislated nor a commanded act. The ahadeeth about visiting the grave of the Prophet ﷺ are all weak, according to the consensus of the scholars of ahadeeth, rather they are all fabricated and none of the reliable narrators of the Sunan related any of them, nor have any of the imams used any of them as evidence.” [ Majmoo’ al- Fatawa vol.27, pg 26.]
source: islamkingdom