This is a LONG answer — the question hits home on a classic example of the English language’s tendency for exceptions. It causes lots of confusion even for native speakers.
Both forms are grammatically correct (contrary to the insistence of some British grammar purists). The first one (“What happened?”) is the one most of us would likely ever to need in normal life.
The short practical answer:—
- Use “did” when we knew something had happened but wanted more details. In other words, treat “did” as an emphatic to express some kind or level of surprise, shock, disbelief, doubt or need for confirmation. It’s like, if that didn’t happen, what actually happened?
The short technical answer:—
- The structure depends on who or what is the subject of the question.
The full(er) story:—
It’s the difference between a subject question and an object question. A subject question are those about the subject of a sentence, and an object question, the object of a sentence. To figure out which is which, we have to invert the question into a normal sentence.
The general rules:—
- Subject questions don’t take auxiliary verbs, and the verb is in its correct tense if it were a sentence.
- Object questions take auxiliaries like ‘do,’ ‘does,’ ‘did’ or similar, and the verb is in the infinitive form.
In other words, if the question word (e.g. what, who, etc) is the subject of the sentence, don’t add ‘do,’ ‘does’ or ‘did.’
For example, take the basic sentence “Jack wrote the essay.”
Ask yourself — who wrote the essay?
This is a subject question. Jack (‘who’) is the subject of the verb wrote. When a question is about the subject, we use the verb in its correct tense without an auxiliary verb.
Ask yourself — what did Jack write?
This is an object question. Although ‘Jack’ is the subject of the question itself, the object of the verb write is actually ‘the essay’ (represented by ‘what’). When a question is about the object, we use an auxiliary verb (‘did’) and an infinitive (“write”).
Then in a similar way:—
“What happened?”
This is a subject question because ‘what’ (the question word) is the subject of the verb happened.
“What DID happen?”
This is an object question because ‘what’ ISN’T the subject of the verb happened. In a strange way, this kind of makes ‘what’ the object while ‘did’ is the subject of the verb happen.
The reason for regarding “What happened?” as more correct:—
The usual explanation from grammar purists is that there cannot be be a sentence without inverting the auxiliary verb order. In other words, inverting “What did happen?” into a normal sentence doesn’t produce a grammatically correct sentence, so the interrogative must be incorrect too.
That rule is actually meaningless in actual language operation, even if it sounds sensible enough. “Did it rain?” doesn’t have a subject in it, yet the auxiliary verb ‘did’ is invertable to form a proper sentence (“It did rain”) as much as “It rained” is the usual correct sentence form. In short, the rule actually fits for both sentences.
Some more examples to illuminate:—
Below are object questions. — The verb and its subject are boldfaced. Objects are italicised. We use the normal interrogative structure with two verbs (infinitive + auxiliary):—
- What did he do? — (Object: what; Subject: he; Infinitive Verb: do)
- Where does he live?
- When will we arrive?
- Where did it happen?
- Why did it happen?
- Where did it happen?
- How did it happen?
Below are subject questions. — The verb and its subject are boldfaced. We use a different interrogative structure: the tense in correct form but no auxiliary verb.
- What killed him?
- Who killed him?
- Who ate the last piece of cake?
- Who wants to go to the cinema?
- What kept you so late?
- What happened?
(Honestly, I wish I never learnt this grammar crap and just went by the “short practical answer” up top of this answer because the results mostly came to the same thing in practical life.)