First off, not all inverters inherently synchronize with a utility feed. Only those identified as "grid tie" inverters have the necessary transducers to do so.
So that's what they do, they have transducers that determine the phase angle of the utility sine wave, and that information is fed to the firing circuits of the output transistors to match and make sure the power only flows one direction. It's all just software at that point.
__________________
** All I every really wanted to be, was... A LUMBERJACK!.**
Here is my write up on the subject regarding simple DIY designs I designed and developed myself.
Its more of a low tech analog based design but the concepts have proven themselves very reliable and easy to adapt to many types of input power sources and utility service connections.
Good Answers: