If the Masjid becomes ruined (i.e.,dilapidated) and similar to it, due to a flood or other than it, it is legislated for the people of the area where the Masjid is located to rebuild it and establish prayer in it. This is due to the statement of the messenger of Allah:
"Whoever builds a Masjid for Allah, Allah builds a house for him in paradise." (Al-Bukhari no. 450 and Muslim no. 533)
Also, due to the statement of A'ishah:
"The messenger of Allah ordered the building of Masjids in the Duwr and that they be cleaned, scented and made nice."(Ahad 6:279 and Abu Dawud no. 455)
This was recorded by Ahmad, Abu Dawud and Ibn Majah with a good chain of narration. The word Duwr here means among the tribes, in the neighborhoods and similar places. The Hadiths concerning the virtue of building Masjids are numerous. If there is another Masjid in the vicinity (of the Masjid) sufficient (for the people of that area), its stones and rubble may be moved and used to reconstruct another Masjid in another place or another land that needs that. It is the duty of the authority in the land where the mentioned Masjid is, whether it be a judge or governor or shaykh of a tribe or anyone similar to them, to help in this. They must assist in the transporting of these materials (from the old Masjid) to be used in the construction of the Masjids that are in need of them or in selling them or using them for the benefits of the Muslims. It is not the right of anyone from the people of the land to take anything from these materials (left at vacant Masjids) except with the permission of the authorities.
If these was a grave in the Masjid, it is obligatory to dig up the grave and move whatever it contains of bones - if they are present - to the graveyard of the land. Hence, a grave should be dug for it and it should be buried in the graveyard, because it is not permissible islamically to place graves in the Masjids or build Majids over graves.
The reason is that this is a means that leads to shirk (associating partners with Allah) and Fitnah (evil trials or temptations) due to the graves. This is exactly what happened in most of the lands of the Muslims from times long ago, due to exaggeration concerning the people of the graves. Indeed it has been confirmed that the prophet ordered the digging up of the graves that were at the place where his Masjid was built. Also, it is confirmed in the Two Sahihs (Al-Bukhari and Muslim) that he said:
"May Allah curse the Jews and the Christians. They took the graves of their Prophets as Masjids." (Al-Bukhari no,1330 and Muslim no.529)
And in Sahih Muslim it is reported from Abu Murthad Al-Ghanawi that the Prophet said:
"Do not pray towards the graves and do not sit on them." (Muslim no.972)
Also, in Sahih Muslim it is reported from Jundub bin 'Abdullah Al-Bajali that the Prophet said:
"Verily, those who were before you all, used to take the graves of their Prophets and righteous people as Masjids. So, do not take the graves as Masjids, for verily I prohibit you all from that." (Muslim no. 532)
In the Two Sahihs it is reported from Umm Salamah and Umm Habibh that they mentioned to the Prophet a church that they saw in Ethiopia and what was in it of pictures. So, he said:
"These are people that whenever a righteous man dies among them, they build a Masjid over his grave and make these pictures in it. These people are the worst of creation with Allah." (Al-Bukhari no.427,1341 and Muslim no.528.)
In sahih Muslim it is reported from Jabir bin Abdullah Al-Ansari that he said:
"The messenger of Allah prohibited that the grave be plastered,sat on or built over." (Muslim no.970)
At-Tirmithi added in his version, with an authentic chain of narration, that he also forbade that it (the grave) be written on. Thus, these Hadiths, and what has been reported with the same meaning, all prove the forbiddance of building over graves, constructing Masjids over them, praying (As-Salah) over them, plastering them and similar things that lead to shirk making association of those buried in the grave with Allah.
In addition to this is the placing of curtains over the graves, writing on them, pouring perfumes over them and burning incense at them. All of this is from the causes of exaggeration concerning the graves and shirk with their people (the deceased). Thus, it is obligatory upon all of the Muslims to beware of this and to ward against it, especially those who are in authority. For verily it is a greator obligation upon them and their responsibility is even greater, because they have more power than others to remove these evils and other evils. Due to their laxity and the silence of many of those who are considered knowledgeable, these evils have increased and spread in most of the Islamic lands. For this reason Shirk has occurred and the people have fallen into what the people of the days of pre-Islamic ignorance fell into, who worshipped Al-Lat, Al-'Uzza, Manat and other idols. They said just as Allah mentioned concerning them in His Noble Book:
"These are our intercessors with Allah." (Yunus 10:18)
And they said:
"We only worship them so that they may bring us near to Allah." (Az-Zumar 39:3)
The people of knowledge have mentioned that if the grave is placed in a Masjid, it is obligatory to dig it up and remove it from the Masjid. If the Masjid was put there last after the grave was already present there, the Masjid should be torn down and removed, because evil has occurred from its construction,. This is because the Messenger warned his ummah (the Muslim nation)against building Masjids over graves and cursed the Jews and Christians because of that. He forbade his ummah from imitating them (the Jews and Christians) and he said to Ali:
"Do not leave a picture except that you erase it, nor an elevated grave except that you level it." (Muslim no. 969)
Allah is the One whom we ask to correct the conditions of all the Muslims and provide them with understanding of his religion. We ask him to make the leaders of the Muslims righteous, gather their word upon piety and help them to rule according to his Shari'ah(Islamic law). We ask him to make them beware of that which opposes the Shari'ah. Verily, he is most kind, most generous.
May Allah send blessings and peace upon our prophet Muhammad, his family and his companion.