This can be easily explained utilising syntactic theory, where it can be seen to be due to the interaction between the do-support of negation and Wh-questions, and the contraction of the auxiliary and the negation when they appear next to each other. Indeed this supports the idea that we have auxiliary verb movement in WH-questions.
In essence, we have a number of rules (simplified here so as not to go too far into syntactic theory):
- The subject should appear before the verbal phrase (which includes the auxiliary verb, the lexical verb, adverbs and the direct and indirect objects).
Verbal negation should appear within the verb phrase, after the first auxiliary verb and before the lexical verb. If there are no auxiliary verbs, the auxiliary "do" is added (do-support).
- John has come to school - John has not come to school.
- John came to school - John did not come to school.
In WH-questions, the first auxiliary verb moves and appears between the Wh-word and the subject. Again, if there is no auxiliary, we have do-support.
- Who has John seen - John has seen Mary
- Who will John have been seeing - John will have been seeing Mary
- Who did John see - John saw Mary
So if you do the contraction of the auxiliary verb do with the negation before the movement, the negation moves with the auxiliary, and you get
Why didn't he come to work?
But if you do movement first, negation remains in its place before the main verb, and you get
Why did he not come to work?