All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His Slave and Messenger.
You should know that the Mosque of the Prophet, sallallaahu ‘alayhi wa sallam, was not built on the grave. Rather, it was built in the lifetime of the Prophet, sallallaahu ‘alayhi wa sallam. What happened is that the Prophet, sallallaahu ‘alayhi wa sallam, was buried in his home which was at that time adjacent to the mosque. The mosque was expanded during the Caliphate of ‘Umar and then during the Caliphate of ‘Uthmaan, may Allaah Be Pleased with them both, and these expansions did not encompass the side of the houses of the Prophet, sallallaahu ‘alayhi wa sallam, that includes his honored grave. The first expansion that included his home was around 94 AH. Most of the companions had died by then and some of those who were alive condemned the expansion encompassing the houses that include the grave. This was also rejected by the master of the Taabi‘oon, Sa‘eed ibn Al-Musayyib, may Allaah Have mercy upon him. Moreover, even after including it, the grave is not inside the mosque; rather, it is in a separate chamber. That is why the place is preserved and surrounded by three walls. The wall is on a side which is divergent from the Qiblah, that is to say that it is a triangle with the corner in the northern angle so that one does not face it while praying.
Allaah Knows best.