Former England flanker Pat Sanderson scored the late try which gave Worcester their first victory in eight matches this season.
They bounced back from last weekend's 47-16 hammering by Welsh side Ospreys to keep alive their hopes of qualifying from Group C in the EDF Energy Cup.
Sanderson had the final say in an enterprising match where the lead changed hands seven times.
Worcester, staring yet another defeat in the face, took their late chance when Sanderson peeled off the back of a forward drive to dive over the line.
The try enabled their new signing Netani Talei, Figi's World Cup number eight, to celebrate his debut with a win after opening the scoring with an 11th-minute try.
Taking a pass from centre Dale Rasmussen, Talei made an immediate impact, powering in from 15 metres.
A further try from hooker Aleki Lutui, a penalty from fly-half James Brown and a further conversion and penalty from full-back Loki Crichton kept Worcester firmly in contention before Sanderson's late strike.
Quins - beaten only once in their previous 11 matches at the Twickenham Stoop - replied with two first-half tries from winger Ugo Monye, a second-half touchdown from full-back Mike Brown and seven points from the boot of fly-half Chris Malone, who landed two conversions and a penalty.
Worcester's lead from Talei's early try lasted just five minutes when Quins flanker Will Skinner made a break from the halfway line and held off Crichton to send Monye in for his try.
Worcester gained the initiative when Chris Hala'Ufia, the home number eight, was sent to the sin bin by referee Chris White for a high tackle on Mark Tucker.
Brown kicked the resulting penalty and Worcester made the most of the extra man advantage, driving Lutui over for a try, converted by Crichton to put them 15-10 ahead.
Monye finished a move brilliantly created by scrum-half Danny Care, and Malone converted to restore the Harlequins' lead at 17-15 but Worcester edged ahead once more just before half-time with a Crichton penalty.
The home side were back in front after Worcester were temporary reduced to 14 men when winger Marcel Garvey was yellow-carded for deliberately killing the ball to disrupt an attack.
Quins punished them straight away, kicking the penalty into touch and winning the line-out before Malone and Hal Luscombe sent Brown over in the right corner.
Monye narrowly missed out on his hat-trick when Malone's high kick to the left evaded his fingertips.
Harlequins launched a desperate last-ditch offensive to try to snatch victory following Sanderson's match-winning try, but the Worcester defence held firm.