Stockport created the first chance after four minutes when Leon McSweeney pulled a low cross back for Jason Taylor, but his shot was blocked.
Anthony Elding saw a snapshot saved by Spireites keeper Barry Roche, before the visitors replied on 10 minutes when Steve Fletcher scooped a shot over the crossbar from just eight yards.
Chesterfield again went close when Brendan Moloney volleyed narrowly wide, but Lee Richardson's men did go in front on 16 minutes.
Aaron Downes swept over a great cross from the right and there was Fletcher who rose superbly to head home.
Stockport came close to a quick leveller when Adam Proudlock shot straight at Roche from 18 yards, before they did restore parity on 34 minutes.
Michael Rose crossed from the left, Chesterfield's defence didn't clear and Liam Dickinson lashed home the loose ball from close range.
Chesterfield went back in front six minutes after the restart when Jack Lester wriggled free of Jim McNulty and Michael Raynes, before firing home low into the corner.
Just past the hour-mark, the hosts levelled with a well-worked goal.
Again Rose crossed for McSweeney to help the ball on and there was Elding, who tucked home his 15th goal of the season from close in.
Elding then had an effort chalked off for offside, before Jamie Ward missed a great chance late on for Chesterfield to snatch a scarcely-deserved winner.