Stockport stretched their unbeaten run to nine games as they rocked promotion hopefuls Rotherham, grabbing the lead with an eighth minute goal.
Liam Dickinson galloped down the right and managed to squeeze in a deep cross as Graham Coughlan hesitated.
Tommy Rowe managed to slip in at the far post and headed down to beat Andy Warrington.
Rotherham went close to producing a leveller when Mark Hudson cut in from the right and hit a blistering shot that keeper Conrad Logan, on loan from Leicester, fingertipped onto the bar.
Skipper Coughlan powered a header that was cleared off the line and when Danny Harrison crashed a low angled effort Logan produced another impressive save.
Seven minutes before half time the home side were level when Hudson was first to react to Chris O'Grady's flick header and his left foot shot from 12 yards skidded past Logan.
Rotherham could have claimed the lead before the break when O'Grady - ineffective for much of the half - beat the advancing Logan with his header but Jim McNulty was covering on the line to head clear.
Three minutes after the restart Rotherham should have been in front in an amazing goal mouth pinball game.
Dale Tonge tore into the area after a slick build up and lashed a shot that Logan parried away. The ball broke to Derek Holmes whose goalbound header was blocked on the line before the ball hit Logan and somehow looped over the top.
A minute later the visitors were ahead again when Rowe added his second of the game.
Anthony Pilkington beat Paul Hurst down the right and his accurate cross to the near post was glanced in by the 19-year-old Rowe.
On 55 minutes Stockport stretched their advantage and put the result beyond the Yorkshire side.
A through ball from Gary Dicker was perfect for Anthony Pilkington who drilled in the third with a cool low shot.
Seven minutes later Rowe completed his hat trick when he pounced on a loose ball and volleyed in a sweet shot.