Gloucestershire 163 for 8 (Cockbain 89, Sale 3-32, Davey 3-24) beat Somerset 161 for 7 (Gregory 50) by two wickets
Ian Cockbain played the starring role as Gloucestershire eliminated arch-rivals Somerset from the Vitality Blast with a thrilling two-wicket Central Group win in Bristol.
In reply, Gloucestershire reached 163 for eight off the final ball of the match, Smith hitting a boundary off Ollie Sale through midwicket. Cockbain had set up the victory with a brilliant 89 off 57 balls, including nine fours and three sixes.
The result meant a home quarter-final for the hosts, while Somerset were left to reflect on a bitterly disappointing campaign.
They were given a brisk start to their innings by Babar and Steve Davies, who took the score to 53 by the end of the fifth over. Davies was timing the ball sweetly but, having moved to 31 off 19 balls, he pulled a catch to deep midwicket off Josh Shaw.
Gloucestershire then applied their customary mid-innings squeeze, taking pace off the ball with left-arm spinners Smith and Graeme van Buuren, well supported by the wily medium pace of Benny Howell.
Somerset could score only 24 runs between the fifth and tenth overs, the last of which from Howell saw Will Smeed caught at mid-off.
Smith removed Tom Abell to a catch at point in the 11th over and in his next claimed the big wicket of Babar, the Pakistan batsman giving a return catch, having faced 35 balls and hit five fours.
Tom Lammonby reverse-swept a catch to deep cover off van Buuren and Somerset were struggling at 105 for 5.
But Gregory supplied much-needed acceleration, hitting the first six of the innings in the 18th over and following up with another maximum over deep square off David Payne. The Somerset skipper also hit four fours in his 28-ball innings. When he fell to the second ball of the final over, his team could add only two off the last four deliveries.
Sale struck a major blow for the visitors by have Chris Dent caught at deep square off the final ball of the second over of Gloucestershire's reply.
Miles Hammond feathered a catch to wicketkeeper Davies off Josh Davey to make it 20 for 2. That became 27 for 3 when James Bracey mishit a catch to mid-on to give Sale a second wicket.
When Ryan Higgins holed out to deep midwicket off 19-year-old left-arm spinner Lewis Goldsworthy, it was 50 for 4 and much rested on Cockbain, who was looking in dangerously good form.
The first six of the innings was struck by van Buuren off Goldsworthy in the 12th over, but he then fell to a catch at short third-man off Josh Davey.
Cockbain moved to a classy 35-ball half-century, with seven fours. He and Jack Taylor both hit straight sixes off Goldsworthy as the 15th over went for 19 runs.
Taylor then drove a catch to long-off to give Gregory a wicket. Cockbain cleared the ropes again off Sale and Howell hit Gregory for a straight six to leave 24 required from two overs.
Another Cockbain six off Davey made Gloucestershire favourites, but the batsman then cross-batted a catch to long-on.
The last over, bowled by Sale, began with 13 runs needed. Two Howell boundaries followed, but there was another twist then he was caught at long-off.
Three runs were still required when Smith faced the last ball, which he smote over wide long-on for four to loud cheers from Gloucestershire fans in the flats adjoining the ground.
Vitality Blast quarter-finals:
Nottinghamshire v Leicestershire
Surrey v Kent
Gloucestershire v Northamptonshire
Sussex v Lancashire
All fixtures to be played on October 1