What is the reason why the grave of the Prophet (peace and blessings of Allah be upon him) was incorporated into the mosque?
Question

It is well known that it is not permissible to bury the dead in the mosque, and it is not permissible to pray in any mosque in which there is a grave. So why were the graves of the Messenger (peace and blessings of Allah be upon him) and some of his companions incorporated into the Prophet’s Mosque?
Praise be to Allah.It was narrated that the Messenger of Allah (peace and blessings of Allah be upon him) said: “May Allah curse the Jews and the Christians, for they have taken the graves of their Prophets as places of worship.” (Saheeh, agreed upon. Al-Bukhaari, al-Janaa’iz, 1330; Muslim, al-Masaajid, 529). It was narrated from ‘Aa’ishah (may Allah be pleased with her) that Umm Salamah or Umm Habeebah told the Messenger of Allah (peace and blessings of Allah be upon him) about a church that they had seen in Abyssinia (Ethiopia) and the images that were inside it. He (peace and blessings of Allah be upon him) said: “Those are people who, when a righteous slave or a righteous man among them died, they would build a place of worship over his grave and put those images in it. They are the most evil of creation before Allah.” (Agreed upon; al-Bukhaari, al-Salaah, 434; Muslim, al-Masaajid, 528). Muslim narrated in his Saheeh that Jundub ibn ‘Abd-Allah al-Bajali said: “I heard the Messenger of Allah (peace and blessings of Allah be upon him) say, ‘Allah has taken me as a close friend (khaleel) as He took Ibraaheem as a close friend. If I were to take anyone among my ummah as a close friend, I would have taken Abu Bakr as a close friend. Those who came before you took the graves of their Prophets and righteous people as places of worship. Do not take graves as places of worship, for I forbid you to do that.” (Muslim, al-Janaa’iz, 970). 
Muslim also narrated that Jaabir (may Allah be pleased with him) said: “The Messenger of Allah (peace and blessings of Allah be upon him) forbade plastering over graves, sitting on them, and building over them.” (Muslim, al-Janaa’iz, 970). These saheeh ahaadeeth and others all indicate that it is haraam to build mosques over graves, and that those who do that are cursed. They also indicate that it is haraam to build over graves, to erect domes over them or to plaster over them, because these actions lead to shirk involving the graves and worshipping their occupants instead of Allah, as happened in ancient times and is still happening today. So the Muslims, no matter where they are, must beware of doing that which the Messenger of Allah (peace and blessings of Allah be upon him) has forbidden, and not be deceived by what many people do. For the believer must follow the truth and truth may be known through evidence from the Qur’aan and Sunnah, not by people’s opinions or what they do. The Messenger Muhammad (peace and blessings of Allah be upon him) and his two companions [Abu Bakr and ‘Umar] (may Allah be pleased with them) were not buried in the mosque, rather they were buried in the house of ‘Aa’ishah, but when the mosque was expanded during the time of al-Waleed ibn ‘Abd al-Malik, the room was incorporated into the mosque, at the end of the first century AH. His action does not come under the ruling of burial in the mosque, because the Messenger (peace and blessings of Allah be upon him) and his two companions were not moved to the land of the mosque, rather the room in which they are buried was incorporated into the mosque because of the expansion. No one can use this as evidence that it is permissible to build structures over graves or to take graves as places of worship, or to bury people inside mosques, because of the saheeh ahaadeeth quoted above which forbid that. What al-Waleed did is not evidence for going against the proven Sunnah of the Messenger of Allah (peace and blessings of Allah be upon him).
And Allah is the Source of strength.