The last part of the gut (the rectum) is effectively a stretchy squeezy hosepipe held closed by a doughnut at the end (the anus). In someone who is not constipated, the last part of the gut sits empty most of the time. Then the gut higher up pushes down some poo, you feel it pushing against the walls of the rectum, and you know that before long you'll need to poo. When someone is becoming constipated, for whatever reason, the poo in the rectum is building up faster than it is coming out. This can happen even with regular pooing, if the regular poo isn't enough volume to make up for what is coming into the rectum. The more poo builds up, the longer it sits in the rectum. Whilst it sits there, more water gets sucked out of it by the gut walls, drying it out. So the increasing amount of poo gets harder.
So you now have an increasingly big, increasingly hard, mass of poo sitting in a squeezy stretchy hosepipe. Which stretches. As it stretches, it gets less good at pushing. As it gets used to being stretched, this interferes with signals going back up to the brain from the rectum, so it can go from constantly sending a message that poo is ready to come out, to never sending that message. And not being able to tell when there is a natural wave of pushing coming through the gut, which can help poo come out.
Then you have the problem of the mass of poo pushing on the bum hole. This can have a similar effect to a baby's head pushing on the cervix in labour...it can thin it and stretch it out. So now it's harder to push out hard rocks of poo, but also harder to hang onto squishy poo, which can cause accidents.
But why is there squishy poo, I hear you ask? Okay, whilst all this is going on, there's still food and drink going into the gut, and all the digestive juices. This means that higher up the gut, before it turns into poo as we know it, there is more liquid poo. More and more liquid or soft poo trapped behind a blockage of hard poo stuck in a stretched out hosepipe. So whenever the hard poo shifts a little, the soft poo from higher up squeezes past, and can come out pretty much like diarrhoea.
Obviously I don't know for sure that your child has constipation...but it sounds pretty likely, and if so that's the process. Treatment is based around firstly clearly out the old rocks of poo, and then secondly (and really really importantly) keeping the poo soft and squidgy, with no hard bits, so that the gut can gradually heal back up.
You might find www.eric.org.uk useful.