Like Shahab84 said, fin rot meds will help with his recovery. Ensure your water changes are very regular; since it's a physical injury, do water changes every few days. Make sure you remove any poo or uneaten food, as this will reduce water quality and slow the healing process.
Also, since he was able to get stuck in the castle, i strongly recommend removing the castle from the tank and replacing it with decor that he definitely can't get stuck in. Keeping it in the tank just gives him the apportunity to get stuck again, which will greatly stress him out. Remember, fish can die from stress.
Hope he recovers soon!