Could be something medical, (for sure get her checked out) but it could also be something emotional or mental. Ask her if something is going on that she feels she has no control over.
My niece does this. I am not her parent, but I believe it is an issue of anxiety. My brother is fairly strict with them. She never has accidents at home, only at school. Could definitely be an emotional issue!
For my daughter she only does it when she isn't wanting to stop doing something (reading a book or playing with friends or on the bus home) she doesn't do it at school at all.
I would consult her pediatrician. Could be a number of things but I would definitely get her checked out