It doesn't usually happen. Posthumous defecation can be a result of relaxation of the anal sphincter. If there is no stool there, then it isn't going to come out. Additionally, the normal passage of a stool requires the large intestine to do a fair bit of work - the necessary muscles don't function after death.
Some causes of death are more likely to produce posthumous defecation. For example, hanging will allow gravity to assist. Spasms in the muscles of the large intestine might also cause defecation, but this is not a rule.
In cases where death is expected e.g. execution, fear might lead to loosening of the bowels.
All in all, a slightly unsavoury subject, but the perpetrator is beyond embarrassment so I suppose no one gets hurt.
Posthumous flatulence, is more common, so I hear.