Social Psych class explanation: when a person experiences cognitive dissonance (brought on by behaving in a way that is contrary to their existing self-perception), they are prompted to reduce the dissonance by changing their attitude or their beliefs to allow themselves to regain their emotional balance, so to speak. The guy in the story is a school teacher and is in charge of children during class time. I would imagine that if he had taken the time to consider it, he would have stated that he would not hurt a child. Then he did hurt a child - creating dissonance, which he can effectively reduce by deciding* that his initial explanation of being prompted by God is, in fact, what actually happened - because he would not otherwise hurt a child. According to the theory (and beaten into my head by my instructor), he is now committed to that belief and has incorporated it into his perception of himself. It's unshakable under most circumstances, unless something else happens to force him to re-examine that belief. I misunderstood how sociologists and psychologists use the term before this class.
In other words, you may recognize it for nonsense, and I may recognize it for nonsense, but it's entirely possible, and perhaps even likely, that HE doesn't recognize it for nonsense anymore. He believes what he's told himself.
That's how I understand the theory of cognitive dissonance to work. I rented the textbook for that class and don't have it available to quote it exactly. I probably have the textbook definition in my notes somewhere.
*"Deciding" is the term my instructor kept using, although it's an unconscious process.