All perfect praise be to Allah, The Lord of The Worlds, and may His peace and blessings be upon our Prophet Muhammad and upon all of his family and companions.
The creditor is permitted to authorize the debtor to donate the debt for a charitable cause, such as building a mosque and the like. This is because the money is the former`s, so he may do whatever he wants with it and he is at liberty to authorize someone else to do so, whether that someone owes him or not.
In addition, its stated in [Tohfat Al-Mohatj 5/353]:" If the creditor told the indebted : "Spend a Durham per day from the money you owe me on an orphan and he did as told, then this is valid and the debtor is free from liability."
However, it is conditioned that the debtor approves of this, because authorization is a contract and can`t be valid save with offer and acceptance. Furthermore, it is stated in [Al-Majmooa`, 14/105]:" Authorization can`t be valid save with offer and acceptance, because it is a contract to which the right of each party is attached; therefore, offer and acceptance is obligatory, here, similar to sale and lease contracts. However, the indebted may approve immediately upon concluding the contract or later."
In conclusion, if the indebted person fulfills his part as regards the authorization, then he is free from the debt. And Allah The Almighty knows best.