According to the Gate Control Theory, you can block a painful stimulus (close the gate) and thereby reduce or prevent pain. This theory draws from the presence of two different types of never fibers. The large nerve fibers transfer pressure, non-damaging heat and cold and send signals quickly. The small nerve fibers transfer pain, light touch and extreme heat and cold and send signals more slowly. The idea is to activate the long nerve fibers so they prevent the slower moving small nerve fibers from sending pain signals.
The drawback to these techniques is the tendency for the nerves to habituate (get used to) the sensation in about 15 to 20 minutes. When this happens, either change the place you are working on (move the ice pack) or choose a different activity to do for a while.