As several people mentioned, it sounds like a constipation and control thing. We have an 11-year-old who used to have very similar problems. The leakiness tends to be an issue with constipation. We tried to explain to our son that if it hurts when he poops, it is probably a very hard stool. But if he holds it in, it will get even harder.
If I understand my biology correctly, the longer a stool remains in one's system, the longer it has for the liquids to be absorbed into the body, and the harder it becomes. Then it becomes so hard that subsequent liquids can't be absorbed, and they leak around the hard stool, causing what appears to be diarrhea. That irritates the skin which makes it even more painful to poop. It's a vicious cycle!
Sometimes it would hurt so bad, we would hug our son while he was on the potty and just talk him through it. "I know it really hurts, sweetie. But if you get it out of you, it will feel a whole lot better." A lot of soothing talk, and some tears, would get us past it. Sometimes, too, we'd have "grunt fests" to try to help get it out...okay, one, two, three, RRRRRRR!
I have no doubt some of it was a control issue as well. My husband insisted that our son have a bowel movement at least once a day. If he hadn't pooped during the day, one of us would read to our son while he sat on the potty (for sometimes WAY too long!) in the evening. I'm not sure I can fully recommend this method but one thing was true. If he had at least one BM a day, it was less apt to get impacted.
To this day, it is a continual fight with our son to get enough fruit and vegetables into him to keep things moving. But that is the key. When he was little, I would make sure he drank some juice, sometime during the day, to keep things moving. Grapes, too, were the great regulator for him. He loved them...and we learned to love him loving them, too...lol! I can't tell you how many hours we have spent plunging our toilet, through the years, when our son has plugged up the darn thing!
Anyway, a little love and understanding and hugs when you feel like screaming can be a big help. I've been there! Potty time and meal time are two of the main areas where kids have the most control, even if we wish it weren't so. Take a big breath and try some hugs! And it certainly never hurts to consult with the doctor for other tips.
Best of luck!