All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and messenger.
It is impermissible to perform prayer in a mosque in which there is a grave, whether the mosque is built over the grave or the grave is annexed to the mosque because this may lead to falling into Shirk. Moreover, there are many Ahaadeeth that underline the prohibition of performing prayer in a mosque that contains a grave. If the grave is located within the area of the mosque's yard, within the boundary of the mosque (including the building and all the areas that are considered parts of the mosque), it is impermissible to pray therein as well. Shaykhul-Islam Ibn Taymiyyah said, “If the mosque is built over the grave, it is impermissible to perform prayer therein, both obligatory prayers and voluntary ones. This is forbidden.” [Al-Fataawa Al-Kubra]
However, if the graves are outside the mosque boundary, there is no harm in performing prayer in such a mosque. Shaykh Ibn Baaz said, “If there are graves in the direction of the Qiblah, then to be on the safe side, it is better to separate the mosque and the graveyard with a wall other than that of the mosque or with a path that separates between them. This is better and more prudent in order to be on the safe side so that the praying people would be far from facing the graves. However, if the graveyard is on the right or the left side of the mosque, or at the right or left side of the praying people, then this does not harm them as they are not facing it. Rather, in this case, they are not facing it and they are far from the doubt of facing it.”
As for the Hadeeth narrated by At-Tabaraani that the Prophet, sallallaahu ‘alayhi wa sallam, prayed in Al-Khayf mosque where there are the graves of seventy prophets, we will cite the words of Shaykh Al-Albaani in his book Tahtheer As-Saajid min Ittikhaath Al-Quboor Masaajid. He wrote, “The third misconception is that the Prophet, sallallaahu ‘alayhi wa sallam, prayed in Al-Khayf mosque and a Hadeeth has been reported that the mosque contains graves of seventy prophets. In refutation to this misconception, it should be noted that there is no doubt that the Prophet, sallallaahu ‘alayhi wa sallam, had performed prayer in this mosque. However, saying that Al-Khayf mosque contains graves of seventy prophets is an unfounded claim for two reasons:
First, we do not acknowledge the authenticity of this Hadeeth because it was not narrated by any reporter known for documenting authentic Ahaadeeth, nor was it classified as an authentic report by any of the early reliable scholars of Hadeeth. The (Hadeeth) criticism of this report does not even suggest classifying it as authentic because among its chain of narration there are narrators known for narrating objectionable reports. Verily, this is not inviting to consider the Hadeeth authentic as it is not narrated by other narrators."
He cited evidence on the weakness of this Hadeeth. Then he said, "In conclusion, the Hadeeth is weak and could not be considered authentic. If it is proven authentic, then its refutation is as follows:
Second, The Hadeeth does not clearly state that the graves of the seventy prophets in Al-Khayf mosque are apparent and known to people. Al-Azraqi allocated a number of chapters of his book Taareekh Makkah (pages 406-410) to describing Al-Khayf mosque and did not mention anything about prominent graves therein. It is well-known that the Islamic Sharee‘ah sets its laws according to what is apparent. If there are no apparent or prominent graves in this mosque, performing prayer therein is not prohibited at all because the graves are effaced and unknown to people. In fact, were it not for this report, which is weak as underlined earlier, none would have ever imagined that Al-Khayf mosque contains seventy graves. Therefore, praying in such a mosque does not involve the evils like the case with mosques built over apparent and known graves.” [End of quote]
As for the statements of some Sufis which they cited on the authority of Ibn Hajar in Fat-h Al-Baari, actually, those words were Al-Baydhaawi’s. Ibn Hajar reported such statements on the authority of Al-Baydhaawi. As-San‘aani commented on these statements of Al-Baydhaawi as follows, “Al-Baydhaawi said, ‘As the Jews and Christians used to prostrate themselves before the graves of their prophets out of respect, and perform prayers and other devotional acts in the direction of these graves, they had worshipped these graves as idols. Muslims were forbidden to do the same.’ He (Al-Baydhaawi) added, ‘When one builds a mosque near the grave of a righteous man with the intention of seeking his blessing and not revering him or dedicating acts of worship to him or the like, this does not fall under the relevant warning. I (As-San`aani) say, 'Building mosques near the grave of a pious person out of seeking its blessing is, in fact, a form of veneration. Moreover, the Ahaadeeth deeming it absolutely forbidden to pray in mosques containing graves have a general indication and there is no evidence of the above mentioned justification.
The preponderant opinion is that the reason for the prohibition is to eliminate the means leading to unfavorable ends and avoid the imitation of idol worshippers who venerate inanimate objects that do not hear, benefit, or harm them.” [Subul As-Salaam]
The contemporary Shaykh Dr. Abdul Kareem Khudhayr commented on that in his explanation of Buloogh Al-Maraam when explaining the Marfoo‘ Hadeeth (i.e. Hadeeth traced back to the Prophet ) by way of Abu Hurayrah that reads, “Allaah kill the Jews! They made the tombs of their Prophets as places of worship." He wrote, "Al-Baydhaawi addressed this issue. He adopted a wrong and unacceptable opinion in this regard as he said, ‘Since the Jews and Christians used to venerate the graves of their prophets and prostrate themselves before them as a gesture of veneration, taking the graves as places of worship, their graves took the position of idols. For this reason the Muslims have been forbidden from this action. However, if someone constructs a mosque near the grave of a pious person for the purpose of seeking blessing and not for prostration or performing devotional acts in their direction, he will never be included in this prohibition.’ However, the indication of the Hadeeth’s wording is quite general and includes both cases. It is true that worshipping a dead person and turning to him in supplication instead of Allaah are considered acts of major Shirk. However, Satan may deceive the person and lure him into believing that when he supplicates Allaah or recites Quran over the grave of this pious person while dedicating his supplication and recitation of the Quran to Allaah and not this pious person, is less grave. However, it is still considered a huge danger as it could lead to Shirk.”
Allaah Knows best.