How does peer-to-peer energy trading work?
Leaving aside any regulatory issues as to how billing and access to the network works...

Any micro-generation schemes will feed the grid through an approved grid-tied inverter. When powered up, this monitors the supply for a while, then automatically locks itself to the phase of the grid.  If the primary source is DC, e.g. solar panels, then the inverter will generate AC at the correct phase to match the grid.  If the generator is AC, e.g. some wind turbines, then it will need rectifying to DC, before converting back to AC at the right frequency and phase.

There's no way you can carefully route electrons from one producer to one consumer.  The grid doesn't work like that.  Instead, it will all be sorted in the metering and billing.  If one person exports 10kWh and another uses 10kWh, then it's down to the billing system to make sure the right producer gets paid.