DIAGNOSIS That's a power interruption issue. The microwave's clock and memory need constant low-voltage power, even when the unit is 'off.' If turning off a light on the same circuit causes a tiny power flicker, it's enough to reset the microwave's brain.
FIX This is an electrical problem in your house, not the microwave. 1) Check what else is on the same circuit as the microwave. Kitchen lights and microwaves shouldn't ideally share a circuit. 2) The most likely cause is a loose wire connection in the outlet the microwave is plugged into, or in the switch controlling your kitchen light. Turn off the circuit breaker, remove the outlet cover, and check that all the wires are tightly secured under the screws. Do the same for the light switch.
IF THAT DIDN'T WORK If the connections are tight, the issue could be a failing circuit breaker that's overly sensitive to small surges, or a more general problem with your home's wiring that needs an electrician to diagnose.
HEADS UP A loose connection in an outlet doesn't just cause resets—it can arc and create a fire hazard. Even if the microwave seems fine otherwise, fixing this loose connection is important for safety. Don't just live with the resetting clock.