Gloucester almost conspired to throw away a thirteen-point lead on Saturday but held on to beat Harlequins 33-26 in the Aviva Premiership at Kingsholm.
Brett Deacon touched down with three minutes left, just when it looked as though New Zealander Nick Evans would make Gloucester pay for poor discipline.
Gloucester played half the second period with 14 men after props Paul Doran-Jones and Nick Wood were sin-binned, and their transgressions gave Quins a lifeline.
Fly-half Evans scored 21 points from a try, four penalties and two conversions, while England scrum-half Danny Care also touched down.
Evans' opposite number Nicky Robinson amassed 18 points, including conversions of tries from Dave Lewis, Charlie Sharples and Deacon, but Gloucester only just escaped from jail.
They led 26-13 early in the second-half, with a winning bonus point seemingly the most likely outcome of their dominance.
But the home side switched off as Doran-Jones and Wood both collected yellow cards, leaving Gloucester needing to dig deep during the final stages when they should have been comfortably clear.
England centre Mike Tindall returned to captain Gloucester after being rested for the 90-point stroll against Amlin Challenge Cup opponents Rovigo last weekend, but lock Jim Hamilton (ankle) and flanker Andy Hazell (illness) were late withdrawals, meaning starts for Dave Attwood and Luke Narraway, respectively.
Quins, without a win at Kingsholm since 1999, showed 10 changes from the side that crushed Challenge Cup visitors Cavalieri seven days ago, including starts for England pair Care and Nick Easter.
Robinson booted Gloucester into a fifth-minute lead, but Quins responded in opportunist fashion when Care caught the home defence cold.
He reacted quickest when the ball bobbled clear 10 metres inside his own half, and while several Gloucester players stopped in anticipation of referee Tim Wigglesworth's whistle, Care sprinted away with only Tindall embarking on a forlorn chase.
Evans added the extras, yet Gloucester replied just six minutes later through relentless pressure from their forwards that ended with Care's opposite number Lewis sniping over.
Robinson's conversion made it 10-7, and after he slotted two further penalties either side of an Evans strike, Gloucester added their second touchdown.
Sharples, twice denied by last-ditch tackling inside Quins' 22, made it third time lucky when he claimed overlap possession and finished impressively.
Quins were at sixes and sevens, finding themselves heavily outgunned up-front and also lacking pace out wide to cope with the twin threat of Sharples and James Simpson-Daniel.
Evans reduced the deficit by landing another penalty on the stroke of half-time, but Gloucester were good value for their 23-13 interval advantage.
Robinson quickly restored a 13-point lead with his fourth successful penalty, and Gloucester continued to show far greater urgency than Quins, sparked by Robinson's astute decision-making.
It was not all plain sailing for the home side though, as their Scotland international flanker Alasdair Strokosch limped off before Doran-Jones was sin-binned after a scuffle with his opposite number Joe Marler.
Marler escaped any punishment, but despite being temporarily reduced to 14 men, Gloucester continued to test an overworked Quins defence.
Quins knew they had to cash in on their numerical advantage, and Evans did exactly that, scything his way over from 25 metres for a try that he also converted.
Gloucester were suddenly under pressure and their problems intensified when just as Doran-Jones prepared to return, his fellow prop Wood was yellow-carded for tipping Easter out of a lineout.
Evans landed the resulting penalty, leaving Quins just 26-23 behind entering the final quarter of an exciting, if error-strewn, encounter.
Evans, who could do little wrong, then hauled Quins level with his fourth penalty, setting up a thrilling finale as Gloucester looked to reassert themselves on the contest.
And they finally came up trumps when their forwards rumbled over from close range and Deacon was accredited with the touchdown.
Tries: Lewis, Sharples, Deacon
Con: Robinson 3
Pen: Robinson 4
Tries: Care, Evans
Con: Evans 2
Pen: Evans 4
Gloucester: 15 Olly Morgan, 14 Charlie Sharples, 13 Mike Tindall (capt), 12 Henry Trinder, 11 James Simpson-Daniel, 10 Nicky Robinson, 9 Dave Lewis, 8 Brett Deacon, 7 Andy Hazell, 6 Alasdair Strokosch, 5 Alex Brown, 4 Jim Hamilton, 3 Paul Doran-Jones, 2 Scott Lawson, 1 Nick Wood.
Replacements: 16 Olivier Azam, 17 Alasdair Dickinson, 18 Pierre Capdevielle, 19 Dave Attwood, 20 Luke Narraway, 21 Jordi Pasqualin, 22 Freddie Burns, 23 Lesley Vainikolo.
Harlequins: 15 Mike Brown, 14 Tom Williams, 13 George Lowe, 12 Jordan Turner-Hall, 11 Gonzalo Camacho, 10 Nick Evans, 9 Danny Care, 8 Nick Easter, 7 Will Skinner, 6 Chris Robshaw (capt), 5 George Robson, 4 Tomas Vallejos, 3 John Andress, 2 Chris Brooker, 1 Joe Marler.
Replacements: 16 Joe Gray, 17 Ceri Jones, 18 Mark Lambert, 19 Peter Browne, 20 Tom Guest, 21 Karl Dickson, 22 Rory Clegg, 23 Ollie Smith.
Referee: Tim Wigglesworth