J.K.
I have a 4 year old in my daycare that has been doing this also. He's holding his poop, for whatever reason and the accidents happen when he can no longer hold. In the case of my little friend, he doesn't want to stop playing to go. In the case of another little girl I know, she was self conscious about going at school because she didn't want her friend to know she was pooping.
Holding stool can become a big problem if it continues... You can google it for more information, but it can lead to some serious medical issues.
In 2 cases I have known, a stool softener has been prescribed to promote more regular pooping, and that has helped.
Good luck!!