Praying in a building that was built over a graveyard
Question

There is a congregation which prays on the second and third floor of a building that was built over a graveyard. Is it permissible for them to pray there and can they carry on doing so?
Praise be to Allah.If the building is separate, and there are no graves in the place where prayers are offered, then the prayer is valid. Whether they pray on the first floor or the second floor or the third floor, there is nothing wrong with that and the prayer is valid. But if the graves are on the same floor, on the first floor, and they pray on the first floor, then praying at the graves or between the graves is not valid, because the Prophet (blessings and peace of Allah be upon him) said: “Indeed those who came before you used to take the graves of their Prophets and righteous men as places of worship. Do not take graves as places of worship, for I forbid you to do that.” And he said: “May Allah curse the Jews and the Christians; they took the graves of their Prophets as places of worship.”
If there are graves around the building, so long as the ground is completely free of graves and there are no graves in it – meaning the land on which the building was built, but there are graves around it, in front of it, or behind it, or to its right or to its left – that does not matter if the ground itself is free of graves and there are no graves in it.
But if the land was part of the graveyard, and it was seized by force, then praying there is not valid, and no prayers should be offered in the building, whether on the first floor, or the second floor or the third floor, because it is part of the graveyard and the land was seized by force. So it is not permissible for them to remain on that land or to pray there; rather the building should be removed from the graveyard, because this is wrongdoing and is a transgression against the graveyard. End quote.
Shaykh ‘Abd al-‘Azeez ibn Baaz (may Allah have mercy on him).
Fataawa Noor ‘ala ad-Darb (2/1143).