Praise be to Allah.
Haraam wealth is either haraam in and of itself, or it is haraam because of the manner in which it was acquired.
If the wealth is haraam in and of itself, such as extorted and stolen wealth, this it is not permissible for anyone of make use of it if he knows that it was stolen from so and so, rather it must be returned to its owner.
The way in which to repent from extorting this money is to return it to its owner. It is not sufficient for the robber to donate it for building a mosque when he is able to return it to its owner.
But if he is not able to return it to its owner (such as money that has been stolen by some oppressive governments from the people), then there is nothing wrong with spending it on the public interests of the Muslims, which includes building mosques.
Shaykh al-Islam Ibn Taymiyah (may Allaah have mercy on him) said in al-Siyaasah al-Shar’iyyah (p. 35):
If the wealth was taken unlawfully and he is unable to return it to its owners, like much of the government wealth (which has been stolen by the ruler), then helping to spend this wealth in things that serve the interests of the Muslims, such as guarding the borders, covering the expenses of the troops and so on is a kind of helping in righteousness and piety, because what the ruler should do with this money – if he cannot find its owners and return it to them or to their heirs – is to spend it on the interests of the Muslims, as well as repenting if he is the one who did wrong. This is the view of the majority of scholars such as Maalik, Abu Haneefah, and Ahmad. It was also narrated from more than one of the Sahaabah and this is what is indicated by the shar’i evidence.
Even if someone else took it unlawfully, then he must still do that with it. End quote.
As for wealth that is haraam because of the way in which it was acquired, this is wealth which a person earned in a haraam way, such as selling alcohol and dealing in riba, or payments for singing and prostitution and so on. This wealth is haraam only for the one who acquired it. If someone else takes it from him in a permissible manner, there is no sin in that, such as if he were to donate it to build a mosque, or to give it as wages to a worker who did a job for him, or he spends it on his wife or children – it is not haraam for them to benefit from it, rather it is haraam only for the one who acquired it in a haraam manner.
The way to repent from acquiring haraam wealth is to get rid of it, and to spend it on charitable causes, which include building mosques.
Al-Nawawi (may Allaah have mercy on him) said in al-Majmoo’ (9/330):
Al-Ghazaali said: If he has any haraam wealth and he wants to repent and get rid of it – if it has a particular owner then he must give it to him or to his representative. If he is dead then he must give it to his heir. If it belongs to someone who he does not know and he has no hope of finding him, then he should spend it on the public interests of the Muslims, such as bridges, border posts and mosques, and other things which the Muslims share. Otherwise he may give it in charity to poor people. This is what al-Ghazaali said and what others of our companions mentioned, and it is as they said, because it is not permissible to destroy this wealth or throw it into the sea. There is no other option but to spend it on the Muslims’ interests. And Allaah, may He be glorified and exalted, knows best.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) was asked about the ruling on praying in a mosque that was built with haraam money. He replied:
It is permissible to pray in it and there is no sin in that, because the one who built it from haraam wealth may have intended by building it to rid himself of the haraam wealth that he had acquired, in which case it was permissible for him to build this mosque, if his intention was to get rid of haraam wealth. But getting rid of haraam wealth is not only done by building mosques, rather if a person spends that on any charitable project, he has achieved the same aim. End quote.
Majmoo’ Fataawa Ibn ‘Uthaymeen, 12/question no. 304. See also al-Sharh al-Mumti’, 4/344.
And Allaah knows best.