As a gesture of courtesy, yachts should fly a foreign nation's flag when they enter and operate in its waters.
There are no universal rules governing courtesy flag etiquette.
Officials interpret the rules differently from country to country, region to region, island to island or even port to port.
Failing to fly a courtesy flag or flying a courtesy flag improperly may only be considered impolite in some places but in others where it is enforced by local law, officials go as far as impounding passports or imposing fines until the proper flag (which may only be available to purchase locally at great expense) is flying on board.
Flying an undersized, faded or tatty courtesy flag may be considered worse than having no courtesy flag at all in some places.
If you are in any doubt, the best thing to do is observe other yachts from your country and even ask them for guidance.
If the yacht has more than one mast, the courtesy flag should be flown from the starboard spreader of the forward mast.