The Prophet (blessings and peace of Allah be upon him) forbade burying people in the mosque and building mosque over graves; he also cursed those who do that. That was when he was on his deathbed and warned his ummah. He (blessings and peace of Allah be upon him) stated that this was the action of the Jews and Christians and it was also a means that leads to associating others with Allah, because building mosque over graves and burying the dead inside mosques leads to associating the occupants of these graves in worship with Allah, may He be glorified and exalted, because people may come to believe that the occupants of these graves who are buried inside the mosque can bring benefit or cause harm, or that they have some special status which mean that people should seek to draw close to them by doing acts of worship and obedience to them instead of Allah, many He be glorified and exalted.
The Muslims must beware of this dangerous phenomenon and ensure that the mosques are free of graves and are established on the basis of Tawheed and sound belief. Allah says (interpretation of the meaning): “And the mosques are for Allaah (Alone), so invoke not anyone along with Allaah” [al-Jinn 72:18].
The mosque should be for Allah alone, may He be glorified and exalted, and free of any manifestations of shirk, places where Allah alone is worshipped with no partner or associate. This is what is required of the Muslims. And Allah is the source of strength. End quote.
Shaykh Muhammad ibn ‘Uthaymeen (may Allah have mercy on him).
Fataawa al-‘Aqeedah, p. 26.
For more information please see the answer to question number 120214
And Allah knows best.