The socket you plug into may have slightly loose connection sleeves in it. Over time the slight movement of the mouse cord (even just from the weight of the cord) may disconnect one pin in the socket. When you unplug and re-plug, it re-establishes the connection.
There is another more common but similar situation. Over time the metal contact points in the socket and pins slowly oxidize just due to oxygen in the air. The metal oxide layer is a poor conductor. At some point in time, that plus minor movement breaks one pin's connection with the socket. When you unplug and re-plug, that action causes metal surfaces to slide over each other, thus scraping the oxide film off. When I have a problem like yours, I often just unplug and re-connect three or four times to be sure to "scrub" the contacts clean, and the problem goes away for a long time. But it may come back, so then you re-do the "scrubbing" thing.