All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His slave and Messenger.
First, building mosques on graves is strictly forbidden; in the same way, it is forbidden to bury a person in a mosque. ‘Aa’ishah narrated that the Prophet said: “Allah Cursed the Jews and the Christians because they made of the graves of their Prophets places of worship.” [Al-Bukhari and Muslim]
Jundub ibn ‘Abdullah said: “Five days before the Prophet’s death, I heard him saying: “Beware of those who preceded you and used to take the graves of their Prophets and righteous men as places of worship, but you must not take graves as mosques; I forbid you from doing that.” [Muslim]
‘Aa’ishah said: “Um Salamah told the Prophet’s about a church which she had seen in Ethiopia and which was called Mariya. She told him about the pictures which she had seen in it. The Prophet’s said, “If any righteous pious man dies amongst them, they would build a place of worship at his grave and make these pictures in it; they are the worst creatures in the sight of Allah.”
Building mosques on graves is forbidden and it is even among the major sins as confirmed by some scholars. Ibn Hajar Al-Haytami wrote in his book “Az-Zawaajir ‘an Iqtiraaf Al-Kabaa-ir” while stating the major sins: “To build mosques on graves, to light them, to make them idols, to make circumambulation around them, to touch them, and to offer prayers facing them (instead of the Qiblah) are among the major sins.”
Second, Islam does not approve of a mosque with a grave. So the one of them that was built first should remain and the other should be removed. Shaykh al-Islam Ibn Taymiyyah said: “All the Imams have agreed upon that a mosque should not be built on a grave for the above Hadeeth of Jundub and that burial in a mosque is also forbidden. If a mosque is built first, afterwards a grave is built in the same place, then the grave should be eliminated, leveling it to the ground or exhuming it if it is new. If a mosque is built after the grave [i.e. the grave was before the mosque], then either the mosque or the grave should be removed. Performing obligatory or voluntary prayers in a mosque built on a grave is forbidden.”
Third, try your best to exhume the bones of the buried person and bury them in a common graveyard of Muslims; otherwise, try to build another mosque.
Fourth, if removing the grave from the mosque is not possible, or it takes too much time, then you may perform prayers in such a mosque, provided no form of Shirk (associating partners to Allah) is performed near that grave, like circumambulation, vows, supplicating the dead person, or seeking help from him. If such acts are performed there, then performing the prayer is forbidden. Allah Says (what means): {And it has already come down to you in the Book that when you hear the verses of Allah [recited], they are denied [by them] and ridiculed; so do not sit with them until they enter into another conversation. Indeed, you would then be like them. Indeed Allah will gather the hypocrites and disbelievers in Hell all together.} [Quran 4: 140]
Know that sitting in a place where evil is practiced is not allowed, since whoever sits there voluntarily, without forbidding such practice, is actually sharing in that act. In such case, you may perform the prayer anywhere on the earth.
Allah knows best.