Nolann Le Garrec kicked a late penalty to earn Racing 92 a first win in this season's Heineken Champions Cup and deny battling Harlequins.
With three players in the sin-bin, Racing trailed by two points when Le Garrec kicked his 77th-minute penalty at Paris La Defense Arena.
The French side had led by 18 points after tries from Gael Fickou, Kitione Kamikamica and Francis Saili.
Quins hit back through Danny Care, two Cadan Murley tries and a penalty try.
Looking to avenge last month's 14-10 defeat by the English Premiership outfit, Fickou dived over for the opening try in the fourth minute to give Racing a perfect start.
Marcus Smith, on his return to action after recovering from an ankle issue he suffered in November, reduced the deficit with a penalty, before Kamikamica ran a terrific line to burst through the Quins defence and stretch the hosts' lead.
It got worse for the visitors two minutes after the break as former Quins centre Saili latched onto a pass to glide to the whitewash as Finn Russell's boot boosted Racing's control of the match.
But it soon turned as Care sniped his way to the line from close range and Smith's influence began to grow. The England fly-half kept the Racing defence busy before turning on the accelerator and feeding Murley for his first try.
Smith made a similarly dangerous break for the third try as the ball was recycled and fired wide for Murley to cross again.
With Racing duo Donovan Taofifenua and Eddy ben Arous already in the sin-bin for the tense finale, Russell was then penalised for a deliberate knock-on, with Joe Marchant poised to score for Quins under the posts five minutes from time.
The Scotland number 10 joined his team-mates on the sidelines as Quins were awarded a penalty try.
With a two-point and a three-man advantage, it seemed inconceivable the Premiership side would lose, but they were unable to gather the restart and conceded the penalty which Le Garrec knocked over.
Racing are eighth in Pool A with one game remaining, while Quins, who claimed a losing bonus point, are one place higher in seventh.
Line-ups
Harlequins: David; Murley, Marchant, Esterhuizen, Morris; Smith, Care; Marler, Walker, Louw, Lewies, Herbst, Wallace, Evans, Dombrandt (capt).
Replacements: Head, Els, Kerrod, Hammond, Lawday, Gjaltema, Beard, Edwards.
Racing 92: Gelant; Taofifenua, Saili, Fickou, Imhoff; Russell, Le Garrec; Gogichashvili, Chat, Gomes Sa, Woki, Palu, Diallo, Chouzenoux, Baudonne.
Replacements: Narisia, Ben Arous, Nyakane, Hemery, Kamikamica, Gibert, Klemenczak, Spring.