My grand daughter who has been potty trained for ages started having this problem this year. (she was 5.5 yrs old.) Turned out, she was afraid of the toilet because the toilets at school and in stores were so loud when she had to flush them. She started holding it so long that she got a urinary tract infection from doing that. She never had any symptoms that we could have known she had one. 10 days on an antibiotic and she was good as new.
There are several things that can cause a child to have accidents but just because they are potty trained doesn't mean something can't trigger them to have accidents all of a sudden.
After the school nurse showed her how loud each toilet was, they were able to find one that wasn't as loud as the others and she went to that one each time the class went to the restroom. We told her that it was her responsibility to make good choices to get up and go like a big girl. When you get mad at her, it doesn't solve the problem, but lets her know that if she wants attention (good or bad) this will get it. Make it her decision and show her that good choices make life so much easier.
I was so glad to learn this approach from the nurse and we were over it in no time.
Good luck.