Yes, the sentence is grammatically correct. It uses "because" and "when" appropriately. However, for improved clarity, you might consider restructuring the sentence slightly:
"I answered the question wrong on the exam because my teacher didn't teach me that."
This revision maintains the same meaning but organizes the elements in a slightly clearer way.