Offering Salah in the Masjids containing graves is not permissible and such graves must be exhumed and the remains must be moved to the public cemetery. Each burial is to be in an individual grave as is the case for all graves. It is not permissible for any grave to remain in a Masjid, whether of a grave of a pious person or any other.

The Messenger (صلى الله عليه وسلم) prohibited and warned against this cursing the Jews and the Christians for this practice. It was authentically reported that the Prophet (صلى الله عليه وسلم) stated:  May Allah curse the Jews and the Christians, for they have taken the graves of their Prophets as places of worship  ‘Aisha (رضی اللہ عنھا) said: “He (صلى الله عليه وسلم) warned against what they did.”(Agreed upon by Al-Bukhari and Muslim).  When Um Salamah and Um Habibah informed the Prophet (صلى الله عليه وسلم) about a church in Abyssinia (Ethiopia) which contained paintings, he said: When a righteous person dies there, they would build places of worship and paint these paintings; they are the worst of creatures in the sight of Allah. (Agreed upon its authenticity).

Moreover, the Prophet (صلى الله عليه وسلم) stated: “Know that those who preceded you used to take the graves of their prophets and righteous men as places of worship, but you must not take graves as places of worship; I forbid you to do so.” Related by Muslim in his Sahih (book of authentic Hadiths)on the authority of Jundub ibn ‘Abdullah Al-Bajali . He (صلى الله عليه وسلم) forbade building places of worship over graves, cursed those who did so, and stated that these are the worst of creatures. So, it is obligatory to warn against this practice. 

It is well-known that whoever offers Salah at a grave, in fact takes it as a Masjid and those who build places of worship, take it as a Masjid. It is obligatory to keep graves away from the Masjids and not to build graves there as the stated by Prophet and to be aware of the curse that Allah (سبحانه وتعالى) placed on those who built Masjids over graves. When one offers Salah in Masjids which contain graves, Satan may deceive him into supplicating the dead, seeking his help, or prostrating to him. Doing so is an act of major Shirk (associating others in worship with Allah). This is one of the practices of the Jews and the Christians and it is obligatory to differ from them and avoid their practices and evil ways. 

However, if the grave is old and a Masjid is built over it, it is obligatory to demolish the Masjid because it is newly built, as held by the people of knowledge, in order to guard against that which may lead to Shirk. May Allah grant us success!  

Reference: Majmoo ‘al-Fataawa Ibn Baaz,