@filmfann , @janbb , and @JLeslie seem to have it backward. Caffeine is a vasoconstrictor—it causes vessels to narrow and thereby reduces blood flow. Increased blood flow is thought to be associated with the onset of headaches, especially migraines. Caffeine also speeds the absorption of other painkillers from the stomach, as pointed out by @PandorBoxx. Specific molecular mechanisms are described in the Wikipedia article on caffeine.
It’s well known that the brain itself is insensitive to pain. The brain coverings (meninges) and other connective tissues in and around the brain, scalp, and neck are where the pain actually is generated. Increased blood flow can irritate or stretch these tissues, causing headache.