Harlequins (20) 29
Tries: Evans 2, Percival Pens: Evans 4 Cons: Evans
Gloucester (14) 31
Tries: Voyce, Molenaar, Taylor, Simpson-Daniel 2 Cons: Taylor 3
Gloucester produced an astonishing recovery to stun Harlequins and set up a LV= Cup semi-final with Cardiff.
Quins needed an unlikely 11 tries plus a victory to qualify but they started strongly and built a 20-point lead.
But Gloucester surpassed the four tries they required to go through, with James Simpson-Daniel went over either side of half-time as the comeback took shape.
In the final minute Tom Voyce scored their fifth try and Tim Taylor's third conversion sealed a famous victory.
Gloucester, 15-0 down after 12 minutes, suffered another blow four minutes later when the latest in a series of infringements at the breakdown resulted in referee Sean Davey sending hooker Darren Dawidiuk to the sin bin.
With the visitors reduced to 14 men, Harlequins struck again as Tom Guest released scrum-half Karl Dickson and he made 40 metres before passing inside for Nick Evans, who ran diagonally before diving over in the left-hand corner.
But Gloucester battled their way back into the match and in the 63rd minute scored their third try, Voyce bursting clear and flicking for Simpson-Daniel to score.
Five minutes from time centre Tim Molenaar grabbed the fourth, Voyce drove in from close range for their final touchdown and Taylor stroked the decisive kick between the posts to take his tally for the match to 11 points.
Gloucester will travel to Wales for their semi-final clash with Cardiff Blues on Sunday 14 March, while Northampton Saints play Saracens at Franklin's Gardens.
Harlequins head coach John Kingston:
"We lost the iron grip we had on the game just before half-time with the two soft tries.
"It's desperately disappointing to lose a match we had dominated in most aspects for most of the game.
"There's very little consolation in the fact that outcome-wise it was fairly meaningless to us."
Gloucester head coach Bryan Redpath:
"We had made a few changes and it looked like that early on but we steadied the ship and got two tries back before half-time.
"We came back and showed great resilience and commitment to win the game and give ourselves a bit of credit back."
Harlequins: Brown, Chisholm, Lowe, Turner-Hall, Drauniniu, N. Evans, Dickson, Jones, Cairns, Andress, Percival, Stevenson, York, McMillan, Guest.
Replacements: Lindsay-Haugue for Drauniniu (64), Luveniyali for N. Evans (74), So'oialo for Dickson (54), Croall for Jones (64), Brooker for Cairns (50), Johnston for Andress (50), Valleyos Cinalli for Stevenson (41), Wallace for York (63).
Sin Bin:Valleyos Cinalli (47).
Gloucester: Voyce, Simpson-Daniel, Molenaar, Vainikolo, Tadulala, Taylor, Lewis, Capdevielle, Dawidiuk, Harden, Eustace, James, Buxton, Hazell, Narraway.
Replacements: Fuimaono-Sapolu for Vainikolo (68), A. Williams for Lewis (68), Somerville for Harden (70), Brown for James (70), Satala for Buxton (57), Azam for Hazell (20).
Not Used: Y. Thomas, Robinson.
Sin Bin: Dawidiuk (16).
Ref: Sean Davey (RFU).