It seems we're led to believe that the moths represent Watashi, the protagonist, in a metaphorical sense. Unfortunately, this is never officially explained.
With regard to The Tatami Galaxy, reviewers and analyzers seem to pull out the fact that moths are meant to symbolize humans. One reviewer called them pitiful (due to the fact that they are nocturnal, yet rely on light to survive). Another blogger pointed out the metamorphosis cycle of a moth.
In both cases, these were drawn to parallels with human beings and, in particular, Watashi:
People too can be pitiful sometimes. When life gets tough and one starts losing his rose-tinted visions of life, he blinds himself from the truth and begins to see what he only wants to see.
After all, moths undergo complete metamorphosis. Something Watashi goes through.
This helps us somewhat to determine the origin of the moths. Every tatami mat room contained a moth (which usually frightened Atashi); upon Watashi's escape from Shimogamo Yuusuisou, the moths escape as well, linking them somewhat to Watashi and representing his escape.
[T]he protagonist's jailbreak out of his endlessly continuing cell of individual rooms is accompanied by a mysterious swarm of moths that overwhelm the city of Kyoto. Officially unexplained, this massive swarm of moths is the simultaneous outpouring of Individual moths from the infinite number of 4.5 tatami mat rooms, each of which contained a single moth.
Why did the moths help Ozu? Hard to say for sure; again, officially unexplained. One viewer I mentioned suggested them as a spirit animal, though:
Then at the end, the moths come down in force as a benevolent instrument to make him possibly the only thing that could stand out more than Ozu. Why are the moths benevolent, and is there some reason why they are chosen? Is it because moths, like him undergo a complete metamorphosis and as such are sort of like his spirit animal?
I believe that in general, The Tatami Galaxy likes to leave some things for the viewer to consider upon finishing the series. There likely is not one single, genuine answer to the question you've posed, but the symbolism of the moth seems to be a good explanation for how and why they appear in the swarm.
(Sorry that reference #3 is a cached version. Its source is this page but it's not fully searchable in the "normal" format.)