It's to do with your sympathetic nervous system (part of your central nervous system).
When you are scared, your body adopts a flight or fight mode. This means that adrenaline is released which helps to divert blood supply to the parts of your body that need it most (i.e if you were being chased by a mad axeman, blood supply is diverted to your legs so you can run faster)
This means that your nervous system, which normally has conscious control of your bladder, is less interested in controlling the sphincters in your bladder. Hey presto - you pee your pants!