Narrated `Aisha:
**Allah’s Messenger (ﷺ) in his fatal illness said, “Allah cursed the Jews and the Christians, for they built the places of worship at the graves of their prophets.”
And if that had not been the case, then the Prophet’s grave would have been made prominent before the people. So (the Prophet (ﷺ) ) was afraid, or the people were afraid that his grave might be taken as a place for worship.**
[Sahih Al-Bukhaari Hadith No. 1390]
Praying in a mosque in which there is a grave falls into two categories:
Either the grave was there before the mosque, and the mosque was built over the grave. It is essential to shun this mosque and not pray therein, and the one who built it must knock it down; if he does not do so, then the Muslim authorities must knock it down.
Or the *mosque was there before the grave*, and the deceased was buried after the mosque was built. In the case the grave must be dug up, and the remains taken out and buried with the people (in the graveyard).
As for praying in such a mosque, it is permissible so long as the grave is not in front of the worshiper, because the Prophet (peace and blessings of Allaah be upon him) forbade praying in the direction of graves.
With regard to the grave of the Prophet (peace and blessings of Allaah be upon him) which is incorporated into his mosque, it is well known that the Mosque of the Prophet (peace and blessings of Allaah be upon him) **was built before his leaving this world, and was not built over his grave**.
It is also well known that the Prophet (peace and blessings of Allaah be upon him) was not buried in the mosque, rather he was buried in his house which was separate from the mosque.
At the time of al-Waleed ibn ‘Abd al-Malik he wrote to his governor in Madeenah, who was ‘Umar ibn ‘Abd al-‘Azeez, in 88 AH, ordering him to dismantle the Prophet’s Mosque and add to it the rooms of the wives of the Prophet (peace and blessings of Allaah be upon him). ‘Umar gathered the prominent people and fuqaha’, and read the letter of the caliph al-Waleed to them. That caused them distress, and they said: “Leave it as it is, that is better.” And it was narrated that Sa’eed ibn al-Musayyib denounced the incorporation of ‘Aa’ishah’s room into the mosque, as if he feared that the grave would be taken as a place of worship.
Umar wrote a letter to that effect to al-Waleed, and al-Waleed sent word to him ordering him to carry out his instructions, so ‘Umar had no other choice. **So you see that the grave of the Prophet (peace and blessings of Allaah be upon him) was not placed in the mosque, and the mosque was not built over it, so there are no grounds for those who try to quote this as evidence that people may be buried inside mosques or that mosques may be built over graves**.