The previous answer was incorrect on almost all points.
We have been discussing this recently in the lab and haven't come up with a definite answer but our best idea is that when you shake it you end up getting lots of tiny bubbles stuck to the walls of the bottle through out the coke instead of all the gas being in the space at the top of the bottle. When you open the lid now the bubbles expand and escape taking a lot of the coke with them.
If the bottle has been sitting still ass the gas will be at the top of the bottle and so will escape easily. The dissolved gas can come calmly out of solution without taking the coke with it.
When a bottle of coke sits still there is a equilibrium pressure that exists between the headspace of the coke bottle and the dissolved CO2 in the coke liquid. Say there is 5 psi in the liquid and there is 5 psi in the empty space at the top of the bottle. When you crack the lid without shaking it the 5 psi pressure vents to atmosphere with the customary whish sound.
When you shake the pop you impart energy to the liquid portion of the pop which warms slightly because of the agitated liquid molecules (mostly water). You can shake a gas and not impart hardly any energy to it, but the liquid portion gains energy from your arm lifting and accelerating the denser liquid. (Sort of like why ice cream melts so fast when you stir it to become a milkshake. The energy of you stirring heats up the ice cream slightly).
Since the coke liquid heats up slightly it imparts some of its heat to the dissolved CO2 bubbles within the liquid. The pressure within the bubble which was 5 psi earlier before shaking, now increases to 6 psi within the liquid. But since the liquid and gas mixture is in a closed container, the gas bubble can't expand. The empty space above the pop still remains at close to 5 psi since the pop can't warm the CO2 space gas that rapidly, like the close intimate contact the dissolved CO2 can.
When you crack the lid after shaking the now higher pressure of the dissolved CO2 gas the now 6 psi is free to expand and does so immediately and the coke liquid is carried out with the gas expanding to atmosphere. If you leave the lid on eventually the 6 psi dissolved gas would equalize again with the lower 5psi headspace and the pop wouldn't fizz beyond the normal whish sound.