The immune system is so complex. I have to wonder if allergies develop when something else is stressing the immune system and it misidentifies the culprit.
Current research seems to suggest that allergies develop when the immune system doesn't find anything to fight and goes looking for trouble (the hygiene hypothesis). Although that is a long term effect and I think it's likely that what you said is true as well, on a short term basis. There's also a big difference in being exposed to the natural environment with lots of bacteria and viruses and plants that don't cause disease in humans, which is really what the hygiene hypothesis is about, and getting sick with human diseases, which is not actually beneficial in any way (the immunity gained this way is narrow, not broad, it doesn't "train" your immune system to fight anything but the particular strain of whatever you got).
So my best guess is that when you live a life indoors for years without much exposure to natural outdoor bacteria/viruses/plants, and then you encounter a human disease, your immune system goes into overdrive and misidentifies the culprit.