Question 317: Is it permissible to build a Masjid (mosque) over a grave out of necessity? Is it permissible to build a Masjid in the graveyard after exhuming the bodies? Is it permissible to install pillars in the grave, while leaving the first floor empty and building the Masjid on the second floor?
Answer: It is not permissible to build a Masjid over graves or exhume the dead to raise a Masjid in their places. The graves have a greater right to the site where they were built, and Muslims must look for another place to build the Masjid. Similarly, it is not permissible to install pillars in the graves or build over them, whether the building is a Masjid or whatsoever. Masjids are not to be built over graves, and the dead must not be disturbed. Furthermore, performing prayers in such Masjids leads people to associate its dwellers as partners with Allaah and exaggerate their status. Therefore, it was authentically reported from Allaah’s Messenger (peace be upon him) that he stated... read more here.