As for building mosques over graves—structures known as “shrines”—this is impermissible. Indeed, the entire Muslim community forbids it, because it is established in the two Sahīh collections that the Prophet, may Allah bless him and grant him peace, said:
“May Allah curse the Jews and the Christians: they took the graves of their prophets as places of worship.”
He was warning against what they had done. ‘Ā’isha said: “Were it not for that, his grave would have been left exposed, but this was avoided lest it be taken as a place of worship.”
It is also reported in the Sahīh that he said:
“Those who came before you used to take graves as places of worship. Beware! Do not take graves as places of worship, for I forbid you to do so.”
It is reported in the Sunan collections that he said:
“May Allah curse women who visit graves and those who build mosques over them and place lamps upon them.”
The leading Muslim scholars agree that prayer in shrines is neither obligatory nor recommended. They also agree that prayer in shrines built over graves and similar sites has no special merit over prayer elsewhere, let alone over prayer in mosques. Whoever believes that prayer at these sites is superior to prayer elsewhere, or that it is better than prayer in some mosques, has broken with the Muslim community and departed from the religion.
Rather, the position of the Muslim community is that prayer in these shrines is strictly forbidden. They do, however, disagree about prayer in a cemetery: is it forbidden, disliked, or permissible? Or should a distinction be drawn between a cemetery whose graves have been dug up and an old cemetery? This disagreement arises from attributing the prohibition to impurity, because the soil becomes mixed with pus from the dead.