As poop moves through the bowel, water is drawn out of it. The longer it stays, the drier and harder it becomes. A poop that waits a day or two too long can become large and painful to pass.
From your child's point of view, the lesson is simple: pooping hurt last time. So when the urge comes, they tighten up and hold it. That is a sensible reaction for a small child, but it means the next poop waits even longer and gets harder still.
When this goes on for a while, the lower bowel can stretch to hold more, and the signal that says "I need to poop" can become weaker. Some children then stop noticing when they need to go, and a few start leaking softer poop around the hard mass without realizing it.
None of this is your child choosing to be difficult. It is why constipation is best handled on two fronts: softening and clearing the poop, guided by your pediatrician, and making pooping feel safe and comfortable again at home.