Ever heard of the fight-or-flight response? When you are looking down the throat of a hungry tiger, your body instinctively goes into red alert. You need to run, so your muscles get a boost, as do your heart and lungs to supply oxygen. On the other hand, everything not immediately necessary gets cast aside. That includes your guts, kidneys and reproductive organs among others.
Your kidneys are pretty much a bunch of tiny pumps attached to each other. Your bladder is a water balloon at the end of the pump. A bigger balloon is harder to pump into, but usually your kidneys can handle it. But now that we are in power-saving mode, suddenly your kidneys are hungry and tired.
It would be like if you only had 110V for a 220V pump. So your body decides to empty the balloon so your pump can work with less energy. Cue the feeling – ‘I need to pee’.
On a side note, people wetting themselves has a bit different reason. When normal fight-or-flight is activated your body typically uses the sympathetic (excitement) nervous system. But when you are surprised, your body actually relaxes – this is because your parasympathetic (relaxation) nervous system also received a jolt. The parasympathetic nervous system happens to cover your bladder sphincters, and with a big enough jolt, the floodgates will open.