Typically, you just superimpose your generated sinewave with that of
the grid and then "try" to bring UP the voltage of your generated sinewave,
and the power "flows" towards the grid. ("Power flow" is not exactly a technically
pure wording but even the power industry uses that term every day)...
The reason they cut off if the frequency of the grid goes too far off is
due to the rules from UL1741 standard and IEEE1547 so the inverter doesn't "island"
and hurt the lineman... Even though the typical inverter and system of less
a few kW would not be able to drive the pole transformer and much less, the whole neighborhood without (most likely) turning itself off anyway.
Tracking a generator is done quite often and is called "Generator support".
You can take something like a Sunny boy and connect it to the output of say,
an OutBack FX inverter and have the FX operate as the "grid" but you have to
be careful of when the loads are small and the output of the inverter
and also the battery voltage rises. In that case, you should add load diversion
or some kind of switch to turn off the Sunny Boy (or equivelent).
boB