This is a well recognised reflex called Arnold's reflex.
It exists because the sensation of the skin in the ear canal is carried by the Vagus nerve. This is the same nerve that supplies sensation to the airways and throat. The signals aren't always fully seperated, so irritation in the ear canal can cause some people to cough. It occurs in about 4% of people, and 3% have it in both ears. The vagus nerve also causes the heart to slow down, controls stomach motion and production of tears - there are similar, less common, reflexes that can cause the heart to slow down (or briefly stop), cause vomiting, or eye watering if something is inserted into the ear canal.
This is slightly different to the sneezing in bright light syndrome (ACHOO syndrome).
This is not so well understood, but is thought to be because the trigeminal nerve nucleus (essentially the main switching point for the nerve brining sensation from the face and inside the nose) is very close to the optic nerve. Some people seem to have some mis-wiring between the areas and sudden intense signals on the optic nerve seem to trigger nerves in the trigeminal nerve nucleus. Interestingly, this is somewhat more common than the coughing reflex, at about 20% of people.