Why? Simply because it shows respect for the country that has allowed you to enter with your boat.
The courtesy flag flies from the starboard halyard and should be raised after you check in. It replaces the all yellow quarantine flag you should fly as you enter the waters of a country (I will admit I am not the best at remembering to do it). This is a practice from the olden days when sailors were restricted to a vessel until a doctor could verify they were not carrying any diseases.
Some counties are very strict and require the courtesy flag to be flown (some even stating only from sunup to sundown), while other countries don’t seem to care. There are a few places that the flag is hard to find…..even on that particular island. Montserrat comes to mind and our guide even said he was having trouble getting some. Since this is a respect thing, try to make sure the flag you are flying is in good shape. How would you feel if you saw a foreign boat flying a tattered USA flag?
I buy courtesy flags either before arriving to the country or once I am there. Most flags cost between $15 and $20 and I think they make nice souvenirs. In order to save money in the cruising kitty, I know cruisers that go to souvenir shops and get a small flag on a stick for $3-$5 and rigging it to fly on the halyard. Others are more creative and take white cloth and color it with Crayons. Once they have drawn and colored the country’s flag they use an iron to melt the Crayon wax into the fabric. As long as you show respect to the country hosting you I am not sure it matters how you obtain the flag.
Just remember this is something we do for the privilege to travel the world!