Answer: The leading jurists are unanimous that a mosque must not be built over a grave, for the Prophet (peace and blessings be upon him) said: “Those who came before you used to turn graves into places of worship; beware—do not turn graves into places of worship, for I forbid you to do so.” It is likewise impermissible to bury a deceased person inside a mosque. If the mosque predates the burial, the situation must be corrected—either by leveling the grave or, if the burial is recent, by exhuming it. If the mosque was erected after the grave, then either the mosque must be removed or every visible trace of the grave erased. A mosque constructed over a grave is not to be used for any prayer, obligatory or supererogatory, for such use is prohibited. Allah knows best.
He has another legal opinion to the same effect; consult it—if you wish—in volume two, page ⦗192⦘ (1).
Shaykh Muḥammad Yaḥyā al-Kandahlawī al-Hanafī states in his work al-Kawkab al-Durrī ʿalā Jāmiʿ al-Tirmidhī:
“As for constructing mosques over graves, this resembles the Jews who built places of worship over the graves of their prophets and notables, and it entails venerating the deceased. If the grave lies along the qiblah wall, the practice further resembles that of the idol-worshippers. The dislike of a grave being on the qiblah side is even greater than the dislike …”