Somerset 263 for 9 (Myburgh 81, Hildreth 48, J Overton 40*) beat Gloucestershire 260 (Dent 100, J Taylor 43, van der Merwe 3-51) by one wicket
Chris Dent hit exactly 100 in holders Gloucestershire's total of 260 all out. Jack Taylor made 43 and Benny Howell 35, while Roelof van der Merwe was involved in seven of the dismissals, three with his left-arm spin, three catches and a run-out.
Somerset looked to be cruising at 166 for 3 in reply, Johann Myburgh leading the way with 81. But his dismissal brought a dramatic collapse to 198 for 9, with only James Hildreth of the other batsmen making an impression.
It looked all over, but Overton (40 not out) and Groenewald (34 not out) had other ideas. The pair shared 6 fours and 3 sixes, surviving just one major run-out scare when wicketkeeper Gareth Roderick failed to gather a throw, before clinching victory with three balls to spare. A crowd of 4,600 enjoyed every moment.
Groenewald said: "I think sharing a match-winning last-wicket stand against Surrey in our last Championship game gave me confidence that we could get the required runs. Jamie Overton played so well that it seemed easier than in the Surrey match.
"Both innings gave me an equal buzz. When you think we also won a T20 game batting second on Friday, I can't think of a better week in my career.
"I just set out to bat properly and concentrate on each ball. The crowd were brilliant and we could really feel them behind us for every ball."
After Somerset had chosen to field, Craig Overton struck a major early blow with an outswinger that clipped the top of Michael Klinger's off stump in the sixth over.
It was 51 for 2 when Roderick cut Lewis Gregory and was caught by van der Merwe, two-handed diving to his left at cover. After ten overs Gloucestershire had progressed to 54 for two.
Dent reached his half-century off 55 balls, with nine sweetly struck boundaries. Hamish Marshall helped take the total to 94 before he was bowled by Peter Trego's second delivery of the game.
The next ball saw Ian Cockbain play into the covers where van der Merwe dived and initially fumbled, but then recovered to effect a brilliant run-out, hitting the stumps at the bowlers' end with a throw while still on the ground.
Howell hit the first six of the game over long-on off van der Merwe before Dent reached a chanceless hundred off 107 balls, having extended his boundary count to 16.
Without adding, the left-hander tried to drive Groenewald and only succeeded in giving another catch to van der Merwe. Howell and Jack Taylor batted sensibly to bring to take the score to 207 with ten overs left. But Howell then advanced to van der Merwe and feathered a catch to wicketkeeper Alex Barrow.
Smith departed to a slog-sweep off van der Merwe and Gloucestershire's last hope of reaching 300 disappeared when Taylor drove a wide ball from Jamie Overton to cover where van der Merwe took his best catch, low to his left.
The hosts' reply started badly as Adam Hose nicked a high catch to Klinger at slip off Matt Taylor and Peter Trego fell to a brilliant one-handed catch by Howell at short cover off Payne.
Jim Allenby and Myburgh took the score to 79 before Allenby, on 29, launched an irresponsible swing at Howell and was bowled. Myburgh reached his half-century off 63 balls, with 7 fours, and had progressed to 81 when losing concentration and lofting Dent's left-arm spin straight to Howell at long-off with 95 still needed.
Lewis Gregory ran himself out attempting a suicidal single to short cover and when Hildreth got a leading edge to be caught and bowled by Smith, having looked untroubled, Somerset were in a hole at 181 for 6.
Another wild swing by van der Merwe saw him bowled by Smith and five wickets had fallen for just 21 runs when Craig Overton was bowled by Howell.
Payne produced a good one to bowl Alex Barrow and it seemed Gloucestershire could celebrate. But Jamie Overton and Groenewald showed great shot selection to reduce the deficit before Overton drove Miles for the winning boundary in the final over of a riveting match.