The Reprehensibility of Praying in a Mosque Built over a Grave—even when One is not Facing the Grave
Be aware that the ruling which deems prayer in mosques constructed over graves to be disliked applies in every circumstance: whether the grave is in front of the worshipper, behind him, to his right, or to his left. In all cases, praying there is disliked. The disapproval, however, becomes even more severe when the prayer is directed toward the grave, for the worshipper then commits two violations: first, performing the prayer in one of these mosques, and second, praying toward the grave itself—an act that is categorically forbidden, whether inside a mosque or elsewhere, by an authentic report from the Messenger of Allah, peace and blessings be upon him, as mentioned earlier.
Statements of the Scholars on the Issue
Al-Bukhari alluded to this in his Ṣaḥīḥ under the heading, “Chapter: What is Disliked concerning the taking of graves as mosques.” He related: “When al-Ḥasan ibn al-Ḥasan ibn ʿAlī, may Allah be pleased with him, died, his wife set up a tent over his grave for a year, after which it was removed. They then heard a caller proclaim, ‘Have they found what they were looking for?’ and another voice replied, ‘No; they despaired and went back.’” He then proceeded to cite further evidence.