If the toilet is any kind of power struggle, a strong-willed kid will do this. One of mine did it because his Dad would get so bent out of shape about it. Seriously - that was the reason. It was a power struggle with Dad and the kid was going to win!
We almost had the same thing with my youngest but I was able to reel Dad in before it got too bad.
So my advice is to relax, stop questioning, and to tell her something like "Hey, I've done everything I can think of to get you to stop pooping in your pants, and it seems like it just backfires. I don't know what else to do. I think it must be uncomfortable and I don't think it's a smart choice, but if you wants to poop in your pants, I guess that's your choice."
Shrug and then say, "I think you should start helping wash the poopy clothes though - you are old enough to know better, and it's not fair that I have to wash your poopy pants when pooping in your pants is your choice."
So - you've set an example about boundaries and given her the power back she seems to be craving. If your kid is anything like any of mine, 80% odd this will work.
GOOD LUCK! I feel for you!!!!
e
p.s. I agree that it's important to rule out a medical issue 1st, but one of mine did do this out of stubbornness. He also had accidents well into elementary school but they happened when he was super excited or scared, and he was mortified - totally different thing. He does have some medical "stuff" that may have caused that issue to take a little longer to resolve than it did in some other kids, but at 4, it was about beating Dad at the potty power struggle at least 90% of the time. It was pretty clear.
p.p.s. If she's having a problem, my approach should also open the possibility of her talking to you - "But Mom, it's NOT my choice! It just HAPPENS!" Then you know you have a different problem. Good luck!