Square wave at line frequency. How does it work?
If we assume that N (neutral) is grounded at the incoming electrical connection point somewhere (where exactly varies from country to country) then N is at the same potential as G and we can ignore R2 and N for now.


L will alternate positive and negative with respect to ground on each mains cycle. On the positive half-cycle the voltage at SEAL will rise until it reaches a little over 5 V. At that point D1 will be forward biased to the +5 V rail and prevent the SEAL voltage rising any further.
On negative half-cycles D2 will be forward biased almost immediately L drops below zero. This, because it's a Schottky diode (see below), will limit the SEAL voltage to -0.3 V on negative half cycles.
R2 and N will work in exactly the same way in the event that the L and N connections are reversed as would easily happen with reversible mains connectors used in North America and most of Europe.


Be careful with this type of circuit.


The G connection would have to be grounded for it to work properly.
Most small resistors are not rated for mains voltage. The 3 MΩ resistor should be made of two or three resistors in series.
You'll have mains on the circuit board. Good layout with adequate isolation is required.



  I think the purpose of this circuit is to generate a square wave with line frequency (50 or 60 Hz), 5V and 50% duty cycle ...


Correct.


  What should be the ratings on the zener diodes?








simulate this circuit &ndash; Schematic created using CircuitLab

I think they should be Schottky diodes rather than Zeners although Zeners would work, provided they had a breakover voltage > 5 V.