What is the ruling on praying in a masjid if there is a grave in it, or in its courtyard, or in front of its Qibla……..?
If there is a grave in the masjid, then prayer in it is incorrect, regardless whether it is behind, in front, on the right or on the left of the performers of the prayer, due to the statement of the Prophet (sallal-laahu-alayhi-wasallam): ”Indeed, those before you [i.e. Jews and Christians] used to take the graves of their Prophets and righteous people as Masaajid, so do not take graves as Masaajid, for indeed I forbid you from that.” [reported by Imaam Muslim]
And that is because praying at the graveside is a means from the means towards shirk and exaggerating (the status) of the inhabitants of the graves. Therefore, it is obligatory to forbid that, acting in accordance with the (two) mentioned narrations and that which has been transmitted (in other narrations) of the same meaning, and (also) preventing the means leading to shirk. [Fataawaa Muhimma Tata-allaqu Bis-Salaah–Imaam Abdul-Azeez Bin Baaz: Page:17-18]