A transponder in most planes look something like this
(source)
The code is changed by turning the knobs beneath the numbers. Lets say you are assigned 5700 as your code but your previous code was 7456. If you are careless or start from the right most knob your transponder will briefly be set in 7700 until the most significant digit is changed. If the radar does a pass (interrogation) at that time ATC will momentarily receive a 7700. Some people say you should put your transponder in standby (SBY) mode when changing codes to avoid this happening.
Some GA planes (and even some larger planes) have newer digital interface transponders like this one,
(source)
These make it a bit harder to pass through the codes since you are not turning knobs but in bumpy air you could accidentally double tap a number. For example lets say you are assigned 7003 in bumpy air. You go to hit the 7 and accidentally double tap the button. Then you type in the 00 and your code is complete by the time you hit the 3.
After some looking it seems that larger aircraft user a transponder like this one,
(source)
In this case you will see a similar case to the first transponder listed. If its not in STBY while switching you inevitable have to pass over 7700, 7600 or 7500 to get to some codes. If an interrogation happens at that time it may cause the results you mention.
In terms of switching to STBY mode, the only documentation I can find about keeping the transponder in altitude reporting mode is this document which states
operators and pilots of the need to ensure that transponders are in the altitude reporting mode whenever their aircraft is on an airport movement area at all airports.
This was issued in May of 2015 and seems to be related to the ground surveillance systems being installed at airports.
The FAA FAR touches on transponder operation in S91.215 and part C reads
(c) Transponder-on operation. While in the airspace as specified in paragraph (b) of this section or in all controlled airspace, each person operating an aircraft equipped with an operable ATC transponder maintained in accordance with Sec. 91.413 of this part shall operate the transponder, including Mode C equipment if installed, and shall reply on the appropriate code or as assigned by ATC.
This section simply states that you must "operate" the transponder on the code assigned while in required airspace. That seems a little vague and does not completely exclude putting it in STBY mode to switch codes.