The whole idea of getting the vaccine is to get the body to produce antibodies (and B and T cells) to the virus without actually being infected.
When an unvaccinated person is infected, the body will produce antibodies to the virus, but it takes awhile. In the meantime the virus replicates unimpeded, perhaps causing severe infection. It's kind of a race between the immune system and the virus. After two shots, antibody levels are high and any virus that enters the body is (hopefully) attacked by the antibodies so that it can't replicate.
After a few months the body slows and eventually stops producing those antibodies. Why waste energy making antibodies to a virus that's not there? However, the body "remembers" having made those antibodies before by producing "memory B cells". These cells contain instructions for making those specific antibodies. The body's reaction time to the virus is much faster then an unvaccinated person, and the infection is usually mild. A third shot causes the body to start making antibodies again.
Now omicron has quite a few changes (32 in the spike protein alone) compared to both the original variant and the vaccine. Only a small percentage of the antibodies to the vaccine attack omicron. Apparently if you are freshly boosted and your antibody levels are high, that small percentage is often enough to prevent illness. If it has been a few months, you still get the fast reaction time from the memory B cells, and illness is mild.
Getting infected with omicron does the same thing as getting a booster. The body ramps up antibody production. In fact, for the long term, a mild omicron infection is better because the new antibodies (and B and T cells) are specific for omicron.
So if you are vaccinated and have had a confirmed (by test) mild case of omicron, your antibody levels will remain high for a few months and you don't need an immediate booster.