It should be stated first of all that Islam forbids all kinds of unlawful means of gaining wealth. It urges people to work and be active and get money through lawful ways. As for the ill-gotten wealth, the ruling on it depends on it source.

In response to the question, Sheikh Zaydibn`Abd Al-KarimAz-Zayd, professor of Shari`ah at faculty of Shari`ah in Riyadh, stated,

When we speak about either money or real estate, as being “unlawful wealth,” we must distinguish between two kinds. First, there is monetary wealth, which is unlawful in its essence, since it is rightfully someone else’s property. This includes stolen and misappropriated properties. The second is the money which is earned through unlawful business transactions, like the money earned through the sale of pork or wine, and the money earned from interest income or through gambling. The first kind of unlawful wealth can never be lawful for the person who stole it, since this wealth belongs to someone else and should be returned (only in cases where it is truly impossible to return that wealth — for instance, because the identity of the owner of the wealth cannot be determined — then the wealth should be spent in charity with the reward of that charity going to the unidentified owner of the wealth).

Consequently, stolen wealth should not be used to build a mosque. If the people who are building the mosque know that the source of the wealth is stolen money, then they should not accept the wealth. Likewise, the misappropriated land should not be used to build a mosque. If the people building the mosque are given a piece of land, which they are sure about its being extorted from its right owners, they should not build the mosque upon that land.

As for the second kind of unlawful wealth (i.e., wealth that was earned through unlawful means, like the sale of pork, interest earnings, or gambling) this money is only unlawful to the person who actually earned it through unlawful means. It is lawful to other parties who later come to receive it.

Accordingly, if the money is used to make a purchase from a shop, the money is lawful to the shopkeeper who receives it. Likewise, if it is given as a gift to someone else or used to support someone else, then that money is lawful for its new recipient. The sin for that wealth is only upon the person who earned it through unlawful means.

Indeed, if those persons were to sincerely repent from their unlawful professions and desist from it, they would not have to give away the wealth they had got from their past earnings. They could lawfully use that wealth for themselves. The proof for this is that Prophet Muhammad (peace and blessings be upon him) had dealings with Jews, who made their money through interest though such earnings are prohibited by the Jewish faith. Allah says,
(That they took usury, though they were forbidden; and that they devoured men’s substance wrongfully.) (An-Nisaa‘ 4: 161)

This proves that the unlawfully earned wealth is only unlawful for those who earned it. Therefore, if such wealth is donated to build a mosque, it may be accepted and used for such purpose.