Shivnarine Chanderpaul guided Derbyshire to the most precious of two-wicket wins over Somerset at Taunton to move out of the relegation zone in Division One of the LV= County Championship.
The visitors reached their target of 244 after lunch had been taken with them just six runs short of victory, moving them a point ahead of their opponents.
Chanderpaul finished unbeaten on 74, having been dropped at mid-on by Craig Meschede off Piyush Chawla on 64, with 21 still needed.
Chawla claimed 5-97 and Jack Leach 3-54, but the two spinners could not quite finish off a Somerset fightback that made for a thrilling finale after they had conceded a first-innings lead of 195.
Derbyshire began the fourth day on 127 for four, needing a further 117 on a pitch offering plenty of turn for Chawla and Leach.
Somerset's hopes were raised when Chawla had Richard Johnson caught behind for a duck off a slow leg-break without a run added.
But Alex Hughes then played positively to contribute 33 to a stand of 68 with Chanderpaul, who was content to largely push ones and twos as he reached a half-century off 86 balls with just four fours.
At 172 for five, with 72 required, Somerset opted to change wicketkeepers, with Alex Barrow taking over behind the stumps so that Craig Kieswetter could bowl his occasional off-spinners.
Kieswetter reclaimed the gloves after sending down three innocuous overs in which he was fortunate not to concede more than 14 runs.
Leach was the most threatening Somerset bowler and he built pressure with 20 successive dot balls from the River End before pinning Hughes lbw on the back foot with the total on 195.
A see-saw contest swung again as Tom Poynton was leg before, trying to pull a Chawla long-hop, and David Wainwright stumped when advancing down the pitch to the India spinner. That was 216 for eight, with 28 needed for victory.
Chanderpaul survived a loud appeal for a catch at short-leg before Meschede spilled a catchable chance, two-handed to his right, to give the experienced West Indies star a vital reprieve.
Tim Groenewald kept Chanderpaul company while lunch was delayed for 15 minutes with a finish imminent, but when they went off at 12:45pm Derbyshire were still six runs short.
Somerset's last throw of the dice, the second new ball which was taken shortly after the interval, failed to reap reward and four leg-byes off Chanderpaul's hip settled a tremendous game.