People die, for whatever reason. Some die of natural causes, some get killed, some kill themselves, but still dead is dead. However, biology does not simply stop at death. Muscles control the retention of waste material in the bladder and rectum, and these muscles relax upon death. In addition, getting killed is often (quite understandably) a rather painful and traumatic experience, and the body prioritizes control of its other functions over keeping one from soiling oneself. As a result, it is not uncommon for a dead or dying person to release their bowels in their last moments.
Girls and women are far more likely to urinate and/or defecate in death, as females typically do so using gravity, while males have to use actual muscle movement to push waste out. In addition, females generally have a lower fear and pain threshold, and are therefore more likely to void themselves due to pain and terror prior to being killed.
The aversion of this trope is what usually constitutes fetish fuel i.e. a female (usually, but in some cases males) releasing her urine and feces as she is killed.