No court demanded he return that money. It was conditional. And he acted without authority to pay back the money without regard to oversight. Congress passed the act that withheld Iran’s money, the timing and amount to be repaid would still justifiably be made by the same body. This was disregarded in a style and with the timing to appear a ransom payment. The Republicans in Congress objected vehemently to this and could conceivably call it an abuse of executive power.
Iran's assets were frozen by President Jimmy Carter.
https://en.wikipedia.org/wiki/Iranian_frozen_assets
https://en.wikipedia.org/wiki/Executive_Order_12170