Jeetan Patel proved an unlikely hero with the bat as Warwickshire overcame a major wobble to record a two-wicket victory over Somerset in their LV= County Championship clash at Edgbaston.
Chasing a victory target of 259, the hosts resumed on day four at 123-2 and appeared to be cruising home as they reached 190-3 with Ian Westwood (32) the only man to depart early on.
But Peter Trego turned the game upside down as he claimed four wickets in the space of 23 balls to put Somerset within sight of a memorable success by lunch.
The all-rounder sparked the Warwickshire collapse by removing Darren Maddy (23) leg before and then accounted for Will Porterfield - who made a fine 84 - and Tim Ambrose who went first ball after offering up a return catch.
Steve Kirby claimed the wicket of Rikki Clarke in the same fashion and, when Keith Barker trod on his stumps taking evasive action to give Trego his fourth scalp, Warwickshire looked doomed at 207-8 with a further 52 runs required.
But Patel - who scored a century against Yorkshire in a first spell at Edgbaston three years ago - then launched a thrilling counter-attack after the lunch interval, blazing four boundaries and three sixes as he compiled 43 off just 36 balls.
With 16 wanted, Patel drove young Somerset spinner, George Dockrell, for a straight six and then two fours before taking a single, which allowed captain Jim Troughton (15 not out) to strike the winning boundary past point.
Nick Compton's century and 167 partnership with Jos Buttler brought Somerset back into contention on day three at Edgbaston.
The pair stretched a slender lead of 35 beyond 200 in their 43 over stand, with Adam Dibble adding a career-best 43 to help the West Countrymen to 354 and set the hosts 259 for victory.
Warwickshire missed an opportunity to wrap things up in three days when skipper Jim Troughton spilled what looked to be a routine chance from Buttler at mid-off.while the batsman was on seven.
The England Lions batsman went on to make 93 from 126 balls before he was trapped lbw by Chris Wright, who also bowled Compton on 133 to leave the visitors 303-8.
Neil Carter's promotion to open paid off with a quick-fire 26 in reply before Warwickshire lost wickets in successive overs from the Pavilion End.
But William Porterfield's unbeaten 57 took the home side to 123 for two and needing a further 136 to win their opening championship fixture.
Somerset opened a 31-run lead over Warwickshire with six second-innings wickets intact by stumps on day two at Edgbaston.
Nick Compton hit 61 not out as Somerset closed on 127-4 after their seam bowlers had earlier kept them in contention.
Warwickshire resumed in the morning on 111-3 in their first innings with the chance to build a big advantage after bowling out the visitors, who had opted to bat, for 147 on day one.
But Somerset chipped away with regular wickets to limit the Bears to 243 all out in the afternoon session. Adam Dibble finished with 3-42 and there were two wickets each for Vernon Philander (2-47), Steve Kirby (2-51) and Peter Trego (2-39).
It could have been even better for the visitors had they not allowed Keith Barker, whose 46 was the top score of the innings, to add 47 with Chris Wright (18no) for the last wicket and pad the home side's lead to 96.
After left-arm seamer Barker (1-22) had removed Marcus Trescothick (10) for the second time in the match, Arul Suppiah and Compton put on 86 for the second wicket either side of tea to take Somerset back into credit.
Warwickshire reasserted control late in the day, Suppiah was run out for 33 and James Hildreth (3) and Craig Kieswetter (8) fell cheaply to leave Compton alongside nightwatchman Dockrell (0no) when stumps were pulled.
Marcus Trescothick's decision to bat first backfired at Edgbaston as Somerset were bowled out for 147, Warwickshire seamer Chris Wright leading the way with 4-47.
Somerset skipper Trescothick was one of two early victims for left-arm seamer Keith Barker as the visitors slumped to 14-3 in the ninth over.
Craig Kieswetter (20), Jos Buttler (18) and Peter Trego (27) all fell after making starts, former England all-rounder Rikki Clarke (2-13) accounting for the latter two.
From 95-8, South African Vernon Philander counter-attacked, hitting three fours and two sixes in his 38 off 47 balls, the top score of the innings.
Philander and fellow seam bowler Adam Dibble (2-24) reduced Warwickshire to 58-3 in reply before fourth-wicket pair Varun Chopra and Darren Maddy added an unbroken 53 to see the home side through to the close on 111-3, just 36 behind.