But if a dhimmī (a non-Muslim under Muslim protection) enters the mosque for a beneficial purpose, scholars hold two opinions, both reported from Ahmad:
The first is that it is not permissible. This is Mālik’s position, because this was the settled practice of the Companions.
The second is that it is permissible. This is the position of Abū Hanīfah and al-Shāfiʿī. There are two opinions within Ahmad’s school and other schools as to whether a Muslim’s permission is required.
He was asked:
“Is prayer valid in a mosque containing a grave, where people gather for congregational prayers and Friday prayer? Should the grave be leveled, or should a barrier or wall be built over it?”
He replied:
Praise be to Allah. The imams agree that a mosque must not be built over a grave, because the Prophet, may Allah bless him and grant him peace, said:
“Indeed, those 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.”