Batteries are loose
Apple designed the Magic Mouse battery compartment according to the official specification for AA batteries. Unfortunately, while Apple engineers were quite precise, the manufacturers of batteries are not. In practice, batteries vary in size, just enough to be problematic in the Magic Mouse.
Given that the batteries are hanging inside the mouse with only the tension of the terminal receptacles to keep them in place, incorrectly sized batteries can easily be pulled by gravity, falling down ever so slightly, just enough to lose contact with the electrical pads.
Wiggling the mouse is enough to re-establish contact between battery and the pads. For a while, until they fall again. Thus the annoying disconnect-reconnect-disconnect-reconnect loop.
Piece of thin cardboard
The solution is to use scissors to cut a piece of thin paper cardboard the size of the batteries. Lay this piece over the batteries before engaging the metal cover on the bottom of the mouse. The cardboard should touch only the batteries, but not the surrounding compartment of the mouse. Your goal is to get the metal door to push the paper up against the sides of the batteries, just snug enough to prevent the batteries from falling downward.
I have found the perfect thickness of cardboard to be the box enclosing a 12-pack of soda cans. A business card might do. You need about a millimeter. Thicker than sheets of paper. Card stock might do it, but may not be thick enough.