The Premiership champions, renowned for turning seemingly lost causes into victories, did it again as they wiped out a 17-point interval deficit.
England centre Joe Marchant scored two second-half tries before wing Cadan Murley’s 69th-minute touchdown completed the recovery mission.
Marcus Smith kicked all four conversions, including the extras following Alex Dombrandt’s first-half try, to leave Gloucester crestfallen.
For the entire opening 40 minutes, Gloucester did their utmost to gatecrash Quins’ Big Summer Kick-Off in front of a 47,000 crowd.
The visitors raced into a comfortable interval lead after tries from impressive number eight Ben Morgan, lock Freddie Clarke and centre Chris Harris, while Adam Hastings converted all three scores and dropped a goal.
There appeared only one winner at that point, but Quins rattled up 21 unanswered second-half points, leaving Gloucester with scant consolation of a losing bonus.
The west country club remain fifth - level on points with Northampton - and in play-off contention ahead of their final regular-season game against Saracens on June 4, but they will see their trip to Twickenham as a huge missed opportunity.
Quins made their intentions clear after they gained a kickable penalty in the opening minute, but Smith opted for touch and an attacking lineout in pursuit of a try, before Gloucester cleared.
The visitors also showed plenty of early endeavour, with Harris prominent and wing Ollie Thorley twice given chances to run at the Harlequins defence.
And Gloucester struck first on the scoreboard, executing one of their trademark driven lineouts to set up an imposing attacking platform before Morgan touched down, with Hastings converting.
But it took Quins just five minutes to respond as centre Marchant’s surging run was only halted by a Louis Rees-Zammit tackle, yet quickly recycled possession created space for Dombrandt to cross from close range, and Smith converted.
A fast and furious opening quarter still had one more scoring act, though, and this time it was Gloucester’s creativity that left Quins reeling.
Full-back Santiago Carreras weaved his way clear and then delivered a superb inside pass to Clarke, who sprinted 30 metres, brushing off Louis Lynagh’s attempted tackle and then celebrating with a Chris Ashton-style dive.
Hastings’ conversion made it 14-7, yet despite Quins’ deficit, they refused to be shackled and full-back Huw Jones’ invention deserved more than a Lynagh knock-on.
Gloucester then showed their opponents how it should be done, handling brilliantly before skipper Lewis Ludlow got behind the home defence and delivered a scoring pass to Harris.
Hastings converted and then landed a drop-goal six minutes before the break, opening up a 17-point advantage, and Quins’ misery was underlined by Dombrandt spilling possession deep inside Gloucester’s 22.
Harlequins needed a rapid response in the second period and they threated after Morgan limped off, but an error allowed a Gloucester counter-attack that was only thwarted by Marchant’s tackle on Thorley.
Marchant brought Quins back into the contest, gathering scrum-half Danny Care’s clever kick and claiming a try converted by Smith that set up an enthralling final 30 minutes.
Marchant then struck again entering the final quarter, with Smith’s conversion closing the gap to just three points and asking serious questions of Gloucester, especially after Rees-Zammit was yellow-carded following a deliberate knock-on.
Quins were now well and truly in the mood, and they went ahead for the first time when the away side ran out of defensive numbers and Murley crossed unopposed, with Smith again converting.