when you get hit on ur toe for example, certain compounds are released that stimulate your nerves. These nerves carry the signal to your brain and you realize the pain.
Now different pain killers work at diferent levels, most of them decrease the levels of the compounds that stimulate your nerves. Some more stronger pain killers can work directly in the brain and decrease the stimulating of the nerves.
So if you have pain in two body parts, taking a pain killer will stop both pains
In a nutshell, a lot of the common painkillers we use do not act on the brain but on the chemical mediators released at the sight of the pain. The more stronger ones such as those based on opiods are a little more complex and involve acting on pathways in the nervous system.