Sayyiduna Abu Sa‘id Khudri (may Allah be pleased with him) said: The Messenger of Allah (peace and blessings be upon him) said: “The entire earth is a place of prostration (mosque) except for graveyards and bathhouses.” (That is, it is not permissible to pray in graveyards and bathhouses.) Imam Darimi (may Allah have mercy on him) was asked: If someone prays in a graveyard, will his prayer be valid? He replied: If he prayed in a place where there was no grave, then the prayer will be valid, and he said: Most narrators have reported this hadith as mursal.
Hadith Referenceسنن دارمي / من كتاب الصللاة / 1428
From these ahadith it is understood that every part of the earth is pure (tahir) unless there is certainty of impurity (najasah); prayer (salah) can be performed on it and tayammum (dry ablution) can also be done with it. The exception to this are those places and locations which have been explicitly mentioned in the ahadith, such as the graveyard (maqbarah), bathhouse (hammam), camel pens, and the roof of the Sacred House (Bayt Allah al-Haram), etc. In a situation of necessity, if there is a mosque in the graveyard which has not been built upon graves, then according to the statement of Imam Darimi rahimahullah, it is permissible to pray in it.