I suspect that an experienced teacher, like @TrifleorBust knows from firsthand experience that the likelihood of children messing around, given half a chance, is massive, whereas the chance of a child with no medical,issues soiling themselves if not allowed to go to the loo, is very small.
In a school where the children are allowed to go to the loo whenever they want, how are teachers supposed to teach a coherent lesson if the class is interrupted every two minutes children getting up to go to the loo? Does the teacher stop teaching each time a child leaves the room - and if so, how would they ever complete a lesson? Or do they carry on, and each child that goes out, misses a different bit of the lesson, and has to ask the teacher to go over it for them, when they get back?
I have had three teenagers, and known plenty of others, and I am pretty sure that, given carte blanche to go to the loo whenever they wanted, they'd have taken the opportunity to go out and piss around.
I think @Trifle's approach is best - say No to most of those who ask, but use discretion if necessary - and the fact that no child has ever soiled themselves in her class proves to me that her approach is sensible.
Teachers do make mistakes - they are only human - and I would hope that the example in the OP is a teacher who made a mistake rather than one being deliberately unkind - but even if it was a mistake, I do think the teacher in question owes the pupil an apology.