I don't know the answer, but here are my thoughts/speculations.
For someone with celiac disease or gluten sensitivity, a gluten exposure does or could do a number of things:
* increases inflammation in the gut, and then, systemically
* washes out bacterial populations, cutting down on the nutrients you get from those guys
* reduces absorption of the foods you eat, in part from foods moving through you too quickly (assuming you get diarrhea), but also from damage to the intestinal lining
* messes with your immune system (probably mediated by reduction in gut bacteria, but also mediated by your immune system thinking you have an invader, the gluten)
* disturbs serotonin production, which occurs largely in the gut (however, I am not sure whether central nervous system serotonin is only made in the raphe nuclei while peripheral nervous system serotonin in the gut, or whether the gut-generated serotonin makes it to the brain).
How does this mess with your head?
If you buy the serotonin hypothesis of depression, and the serotonin created in the gut affects the brain, then that's one way.
The systemic inflammation scenario involves inflammatory cytokines which can cross the blood brain barrier and disrupt normal functioning in the brain (the details of which I'm foggy on).
And the nutrient issue could also come into play if you're at borderline levels, such that a slight dip could cause disruption. I'm especially thinking of the b-vitamins, such as b12 and folate, that are known to be important for neural function.
So those are some mechanisms I can come up with.