The relay coil has inductance. When a voltage is applied to the coil, current starts to flow. The current causes a magnetic field to expand around the coil, but as the field expands, it generates a voltage in the coil that opposes the current. As a result, the current increases slowly (well, slowly is several milliseconds). When you break the circuit and stop the current, the field colapses very rapidly producing a high voltage, high enough to give a shock.