Chris Tremlett was the hero as his four wickets lifted Surrey off the foot of the Division Two table with a 43-run victory over Derbyshire.
Tremlett took the wicket of Tom Lungley late in the final session as Derbyshire were bowled out for 365 in pursuit of a mammoth 408 victory target.
Derbyshire opener Wayne Madsen (105) reignited hopes of a dramatic victory early on day four when he and Garry Park extended their second-wicket partnership to 160.
Park (54) was dismissed on the stroke of lunch, however Madsen brought up his second century in the match to keep the home side in the hunt.
Surrey claimed two vital breakthroughs in the second session when Madsen and Chesney Hughes fell in successive balls to the part-time off-spin of Usman Afzaal (2-33).
Greg Smith (34) and Wes Durston (16) steadied the ship, however when both players fell with the score on 290, Surrey needed just four further wickets for victory.
Wicketkeeper Lee Goddard was then trapped lbw by Andre Nel (3-74) for a 12-ball duck, however Robin Peterson (40) frustrated Surrey and kept Derbyshire in with a fighting chance.
Drama followed when Lungley was forced to retire hurt, however he bravely returned when number 11 Tim Groenewald became Tremlett's third scalp.
Lungley (21) and Steffan Jones (20no) battled on in the hope of forcing a draw, however Tremlett struck with just minutes remaining to hand Surrey only their second win of the season.
Derbyshire closed on 136 for one against Surrey and need another 272 to reach a victory target of 408, but they have never scored 400 in the fourth innings of a match.
Surrey should still be favourites to win their second championship match of the season but they have already lost seamer Tim Linley with a damaged right foot and South African fast bowler Andre Nel and spinner Gareth Batty are carrying injuries.
Mark Ramprakash began the third day needing only 17 to achieve 1,000 runs for the 20th time in his career, but he added only six to his overnight 24 before he dabbed at a wide ball from seamer Tom Lungley and was caught at first slip by Chris Rogers.
His dismissal set the tone for the innings as all the Surrey batsmen got a start but no-one went on to play a major innings on a pitch starting to show signs of uneven bounce.
Batty was handicapped by an ankle ligament problem when Derbyshire started their run chase after tea, although the off-spinner did claim the vital wicket of Rogers, who looked like he was determined to score the 121 he needed to reach 1,000 before stumps.
He got 33 of them before he edged Batty to slip, but Surrey could not build on that as Wayne Madsen added an unbeaten 55 to his first-innings century.
With Garry Park, he shared an unbroken stand of 91 in 24 overs and has set up what promises to be a fascinating, and possibly history-making last day.
Wayne Madsen hit a century to lead a Derbyshire recovery on day two of their LV County Championship clash with Surrey.
The opener made 109 - his third ton of the season - as he combined with wicketkeeper Lee Goddard (67) to put on 137 for the sixth wicket.
The duo's efforts, having come together with the score on 81-6, helped repair some of the damage done by a fine spell of swing bowling from Tim Linley.
The 28-year-old had taken 4-13 before pulling up two balls into his 11th over with a suspected torn ligament in his foot, an injury that will prevent him from bowling again in the Division Two fixture.
Gareth Batty (3-50) eventually ended Madsen's knock before also dismissing Goddard and Tom Lungley, Derbyshire losing their last four wickets for 19 runs to be dismissed for 237.
Surrey opted not to enforce the follow-on, instead going on to reach 50-1 in their second innings by the close to take their lead to a commanding 204.
Rory Hamilton-Brown was the only wicket to fall, edging a drive at Tim Groenewald to Goddard behind the stumps. Mark Ramprakash was 24 not out by the end of the day alongside Arun Harinath (20no).
Mark Ramprakash was just a single run from making another century and hitting a thousand runs for the season as Surrey made a good start at Derbyshire.
The former England batsman got out on 99 for only the fourth time in his 23-year career, although the crowd at Queen's Park thought he had scored his 112th first-class hundred.
Ramprakash raised his bat to acknowledge the applause when he drove Tim Groenewald for his ninth four, but the scoreboard had made a mistake and he was only on 95.
That proved to be an embarrassing error because he got an inside edge into his stumps in Groenewald's next over and walked off 41 short of 1,000 runs for the season.
Groenewald became the first Derbyshire bowler this season to take five wickets in an innings as Surrey closed on 364-9 after a solid start, with Usman Afzaal making 73 and Stewart Walters 53.