Can a Mayor be used to create a Castle?
Short and simple answer: The mayor cannot be placed in a castle.
(From footnote 161 in the Carcassonne Complete Annotated Rules)

If the mayor is in a city that is turned into a castle, you won't get any points from the mayor as there are no banners in a castle. In that case, the mayor will not be returned to you until the castle is scored (that is to say, an adjacent structure is completed).