Why does an electric shock contract the muscle?
Nice Question! Lets first introduce ourselves to the topic i.e. Electrical Muscle Stimulation.


  Electrical muscle stimulation (EMS), also known as neuromuscular electrical stimulation (NMES) or electromyostimulation, is the elicitation of muscle contraction using electric impulses...The impulses mimic the action potential coming from the central nervous system, causing the muscles to contract.


Now, lets come to the main point. Its actually not about flow of electrons or ions, but the change in voltage that plays the pivotal role in muscle stimulation. See this:


  Crystallographic structural studies of a potassium channel have shown that, when a potential difference is introduced over the membrane, the associated electric field induces a conformational change in the potassium channel. The conformational change distorts the shape of the channel proteins sufficiently such that the cavity, or channel, opens to allow influx or efflux to occur across the membrane. This movement of ions down their concentration gradients subsequently generates an electric current sufficient to depolarize the cell membrane.


In short, when you touch a wire, you become a part of the circuit i.e. any 2 points on your hand (or your hand and foot, if you're barefeet) act as part of the circuit i.e. there is potential difference between them, which generates an electric field. This electric field causes the voltage-gated channels in neurons/muscles to open which, in turn, generates an action potential and causes the muscle to contract.



References:


Electrical Muscle Stimulation - Wikipedia
Voltage-gated Ion Channel - Wikipedia