Civil wars pretty much always occur if your faction leader dies without a mature heir, but you still have a couple generals of royal blood (usually the dead king's uncles). Civil wars have other causes as well, but they're pretty much always guaranteed in these situations.
The only way to avoid this is to reload the game before your king died and hope he holds on until his heir matures. And as Deus ret. pointed out, you need to make sure you don't do everything the same as before, as this will cause a new "seed" and therefore reset the chance that your faction leader dies that turn. (@ Malconcath: I confess I have no idea why you're still getting identical results every time; I've not really had that problem myself.) I find that moving the king around and not parking him in the same province seems to give him the best chance of prolonging his life, although this certainly isn't a fullproof strategy.
Of course, I rarely try and avoid civil wars myself, as I find them to be great fun -- nothing like internal upheaval to spice up one's game.![]()
Bookmarks