Ruling on building mosques over graves
Question

Some people say that there is nothing wrong with building mosques over graves, basing that on the aayah (interpretation of the meaning):
“(then) those who won their point said (most probably the disbelievers): ‘We verily, shall build a place of worship over them’” [al-Kahf 18:21]
Is what they say correct? What is the answer to that?
Praise be to Allah.Building mosques over the graves of Prophets and righteous people, or over places connected to their lives, is something which the perfect sharee’ah of Islam came to forbid and warn against, and curses those who do that, because it is one of the means that lead to shirk and exaggeration about the Prophets and the righteous. Reality confirms the soundness of the message brought by sharee’ah, and proves that it is from Allah, and offers definitive proof that what the Messenger of Allah (peace and blessings of Allah be upon him) brought from Allah and conveyed to the ummah is true. Everyone who looks at the state of the Muslim world and the shirk and exaggeration that have come about because of building mosques over tombs, venerating them, adorning them and appointing custodians for them, will realize that this is indeed a means that leads to shirk, and that one of the beauties of sharee’ah is that it forbids this and warns against it.
Among the reports that were narrated concerning that was the hadeeth narrated by the two Shaykhs, al-Bukhaari (1330) and Muslim (529) – may Allah have mercy on them both – from ‘Aa’ishah (may Allah be pleased with her) who said: “The Messenger of Allah (peace and blessings of Allah be upon him) said: ‘May Allah curse the Jews and the Christians, for they have taken the graves of their Prophets as places of worship.’” ‘Aa’ishah said: “He was warning (the Muslims) against (doing) what they had done.”  And she said: “Had it not been for that, the grave of the Prophet (peace and blessings of Allah be upon him) would have been made prominent but he was afraid it might be taken (as a) place of worship.”
In al-Saheehayn it is also narrated that Umm Salamah and Umm Habeebah (may Allah be pleased with them) told the Messenger of Allah (peace and blessings of Allah be upon him) about a church that they had seen in the land of Abyssinia, and the images that were in it. He (peace and blessings of Allah be upon him) said: “When a righteous man died among them, they would build a place of worship over his grave and put those images in it. They are the most evil of mankind before Allah.” (Al-Bukhaari, 427; Muslim, 528)
In Saheeh Muslim (532) it is narrated that Jundub ibn ‘Abd-Allah (may Allah be pleased with him) said: “I heard the Messenger of Allah (peace and blessings of Allah be upon him) say: ‘I stand acquitted before Allah that I took anyone of you as a close friend (khaleel), for Allah has taken me as His close friend, as He took Ibraaheem as His close friend. Had I taken any one of my ummah as a close friend, I would have taken Abu Bakr as a close friend. Those who came before 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 that.’”
There are many ahaadeeth on this subject. The leading scholars of the Muslims, the imams of all four madhhabs (schools of thought) and others, have stated that it is forbidden to build mosques over graves, and they have warned against that, following the Sunnah of the Messenger (peace and blessings of Allah be upon him), sincerely advising his ummah and warning against falling into the same trap as those who came before us, the extremists among the Jews and Christians and their ilk among those of this ummah who have gone astray.
Some people use the story of the People of the Cave to support their specious argument that this is allowed:
“(then) those who won their point said (most probably the disbelievers): ‘We verily, shall build a place of worship over them’” [al-Kahf 18:21 – interpretation of the meaning]
The answer to that is: that Allah has told us about the leaders and powerful people of that time, and that they said these words. That does not imply that He liked that or approved of them, rather He said that by way of blaming them and condemning their action. This is indicated by the fact that the Messenger (peace and blessings of Allah be upon him) to whom this verse was revealed and who was the most knowledgeable of people as to how it should be interpreted, forbade his ummah to build mosques over graves, warned them against doing so, and cursed and condemned those who did that.
If that had been permissible, then the Messenger of Allah (peace and blessings of Allah be upon him) would not have discouraged it in such emphatic terms, and gone to the extent of cursing those who did it, and said that they are the most evil of mankind before Allah. This is sufficient to convince the seeker of truth. Even if we were to assume that building places of worship over graves was permitted to those who came before us, it is not permissible for us to follow their example in that, because our sharee’ah abrogates the laws that came before it, and our Messenger (peace and blessings of Allah be upon him) is the Final Messenger whose sharee’ah is complete and universal. He has forbidden us to build mosques over graves, so it is not permissible for us to go against him. We must follow him, adhering to that which he brought and abstaining from anything from the ancient laws that goes against that or any custom that appeals to the one who does it but goes against that. For there is nothing more perfect than the laws of Allah and there is no guidance better than the guidance of the Messenger of Allah (peace and blessings of Allah be upon him).
 Allah is the One Whom we ask to help us and all the Muslims to adhere firmly to His religion and to follow the sharee’ah of His Messenger Muhammad (peace and blessings of Allah be upon him) in all that we say and do, both outwardly and inwardly, in all our affairs, until we meet Allah, for He is the All-Hearing, Ever-Near. May Allah send blessings and peace upon His slave and Messenger Muhammad and his family and companions, and those who follow his guidance, until the Day of Resurrection.
 From Majmoo’ Fatawa wa Maqaalaat Mutanawwi’ah li Samaahat al-Shaykh Ibn Baz (may Allah have mercy on him), 1/434.
 For the ruling on praying in mosques in which there are graves, see Question no.26312 .