The ruling related to visiting the Prophet’s mosque in Madinah is unknown to many people which is the reason most of them fall into the violation of Shariah. That is why it is extremely important to understand the etiquette of visiting the mosque and his grave.
If a pilgrim wants to visit the Prophet’s mosque before or after Hajj or Umrah, it is permissible for him to do so. However, he should have the intention to visit the mosque and not the grave because travelling to a place for worshipping does not include visiting graves. Travelling with the intention of worshipping only includes three holy mosques: Masjid-Al-Haram, Masjid e Nabawi and Masjid e Aqsa as stated in the following hadith;
“No journey should be undertaken to any mosque except three: al-Masjid al-Haram, this mosque of mine and al-Masjid al-Aqsa.” [Bukhari]
You must understand that traveling to Madinah to perform prayers in Masjid e Nabawi is in no way connected to Hajj or Umrah rituals. It is not a Sunnah act of Hajj or Umrah and visiting the mosque will not perfect your Umrah rituals. However, it is true that there are rewards of performing prayers in this sacred mosque as highlighted in the following Hadith narrated by Abu Hurayrah(RA) that the Prophet (pbuh) said:
"Performing a single prayer in this mosque of mine is better than performing a thousand prayers in any other mosque, except in Masjid Al-Haram. [Al-Bukhari and Muslim]
It is recommended to do what you always do while visiting any other mosque, which is to enter the mosque with your right foot and recite the following:
Bismillaah; was-salaatu was-salaamu ‘ala Rasoollilaah. Allahummaftahli abwaaba rahmatik
(In the Name of Allah; may Allah send blessings and peace upon the Messenger of Allah (pbuh) O Allah, open the gates of Your Mercy before me)
A‘oothu billaahi Al-‘Azeem wa biwajhihi Al-kareem wa bisultaanihi Al-qadeem minash-shaytaan Ar-rajeem
(I seek refuge with Allah, the Most Great, and with His Bounteous Countenance and Eternal Power from Satan, the expelled (from the mercy of Allah)
Then pray two raka’t salah of Tahiyyatul Masjid (greeting the Mosque) and later visit the grave of Prophet Muhammad (pbuh) and the graves of his two companions Abu Bakr and Umar (May Allah be pleased with them) and, greet them by saying Salam to them "Peace be upon you, O Messenger of Allah, and the mercy and blessings of Allah; peace be upon you O Abu Bakr; peace be upon you O ‘Umar."
It is also recommended to visit Masjid Quba and pray two raka’t nafl salah there as the Prophet (pbuh) said:
"Whoever performs Wudhoo‘ in his home and then goes to the mosque of Qubaa‘ and prays two Rak‘ahs there, (the reward of) that would be like the reward of (performing) ‘Umrah." [Ahmad, An-Nasaa’i and others]
Note: It is recommended to consult and seek an opinion from scholars on doubtful Islamic matters.