This article describes how reaction to pain can actually be conditioned, or trained. It uses the theory of classical conditioning, which was first described by Pavlov in his famous dog and bell experiment. An unconditioned stimulus, such as food, causes an unconditioned response, such as drooling in dogs. If a conditioned stimulus, such as the ringing of a bell, is paired with an unconditioned stimulus, then eventually the conditioned stimulus on its own will cause a conditioned response. In this case, the ringing of a bell will cause the dog to drool even though there is no biological reason for a dog to drool at the ringing of a bell.
This is important for the regulation of pain sensitivity. In humans, several types of stressors (unconditioned stimulus) result in an increased tolerance to pain (unconditioned response). These stressors include, well, stress. One example is intense physical activity (think of marathon runners who continue to run and soldiers who do not realize that they are wounded). In animals, even a novel situation can cause a reduction in pain levels. In humans, other stressors that result in reduced pain are loud noises, thermal stimulation, footshocks, and solving mental arithmetic problems and challenging memory tests. So math is good for something at least.
If an initially neutral stimulus is paired with one of these stressors, it could reduce pain by acting as a conditioned stimulus. The conditioned stimulus can be something in the environment. However, it is important to note that not all stressors make changes in pain sensitivity in the same direction. Some will cause less pain to be felt, others more.
As a way to relieve stress and escape from the struggles of everyday life, it is very possible that the substances of addiction are conditioned stimulus that have analgesic (pain relieving) properties. Withdrawal comes with very negative side effects and makes those trying to quit feel miserable. If an alcoholic who is craving a drink gives in to the temptation, he will be blissfully relieved of his withdrawal symptoms, even if it is only for a short time. Therefore, I think it is reasonable to hypothesize that the substance becomes associated with feeling good and pain-free, and becomes a conditioned stimulus. Maybe even the thought of the conditioned stimulus might have analgesic properties.