I would definitely talk to his teacher again. Explain that this doesn’t happen at home, or when he’s out with you. He should know that it is perfectly alright to go to the bathroom anytime he needs. If there is not a single bathroom in his classroom, he might be nervous going to the larger school bathroom alone. Maybe he’s afraid of not finding his way back to his own classroom, or some other unknown fear. If that is the case ask if he could have a “bathroom buddy” so he feels more comfortable.
I would suggest discussing this matter with the teacher, BUT with the principal or the assistant principal present. It is possible that your son and this teacher are mismatched (it happens) and that communication is difficult. Perhaps he just needs to change classrooms, or maybe the teacher needs some sensitivity training.