Harlequins scored two late tries to prevent Bath from going back to the top of the English Premiership.
Centre Jordan Turner-Hall and Welsh prop Ceri Jones struck the crucial blows after number eight Daniel Browne broke the deadlock with the opening try for Bath in the 50th minute. It took something special to win the match and inflict a first away defeat of the season on Bath, who had been knocked off the top of the table 24 hours earlier by Gloucester's victory over Northampton.
The moment of magic was provided by Andy Gomarsall, the 34-year-old former England scrum-half standing in for England number nine Danny Care, whose sublime inside pass opened up the visiting defence to send Jones charging over in the 69th minute. The first half had ended level at 9-9, with Bath fly-half Butch James having three penalties cancelled out by three from Harlequins opposite number Nick Evans before the impressive Browne scored his try in the right-hand corner.
James supplied the pass to hooker Pieter Dixon, whose thrusting run and offload enabled Browne to squeeze over. Harlequins reacted by launching their own offensive in which a deft through kick by Evans almost produced a try between the posts before a quickly-taken tap penalty by Gomarsall resulted in number eight Tom Guest being bundled into touch just short of the line.
The pressure paid off with a Quins try in the 63rd minute. Skipper Will Skinner made the initial break before a long, looping pass from Evans sent Turner-Hall over in the left corner. The second try from Jones settled a hard-fought clash, with former All-Black Evans landing the conversion to put his side seven points clear and edge the kicking duel with South African World Cup-winning fly-half James.
The sides had cancelled each other out in a first half dominated by two well-drilled defences. James put the visitors ahead three times as he landed all his penalties only for Evans to equalise each time as he responded with three from four attempts.
The best first-half try-scoring opportunity had fallen to Harlequins when Gomarsall intercepted a pass from Browne midway inside his own half. The veteran scrum-half's run took him into visiting territory but he lacked the legs to outpace the visiting defence and his pass failed to give young right wing Seb Stegmann the space to finish the move.
Another Quins opportunity went begging over on the left flank when winger Tom Williams failed to find Turner-Hall on the touchline with a pass which flew high but the home side took their chances in the second half to secure victory.
Harlequins chief Dean Richards has warned England players Danny Care, Ugo Monye and Nick Easter they face stiff competition for their places following the victory.
"Andy [Gomarsall] played very well. He pulled it out of the bag. Seb Stegmann also played well and Tom Guest is playing better than ever so it's not easy. The guys who are coming back will have to perform because the guys who are there are playing well.
"I was disappointed in the way we let in their try and we'll look at that and pick out the things we've got to work on. But I was comfortable with the fact that we won against a side that was around the top and playing some good quality rugby. It was an intense battle. It was always one of those games which would come down to the last few minutes."
Steve Meehan, Bath's head coach, said: "Harlequins played some good field rugby but we never gave ourselves the opportunity to force our dominance on the game.
"The penalty count was 14-7 or something like that and when it's that one-sided it's difficult to get any momentum into your game and then we coughed up the ball too often and too early in our attacking play. If we had been able to build on some things we could have come away with the win.
"I'm not going to blame players for the effort they put in - I thought the effort was fantastic right through to the 80 minutes - but there were some confusing decisions out there. I am at a loss to explain some of the things which went against us so there will certainly be some interesting conversations take place between ourselves and those that are in charge this week."
Harlequins: Brown, Stegmann, Barry, Turner-Hall, T. Williams, N. Evans, Gomarsall, Jones, Fuga, Ross, Percival, J. Evans, Robshaw, Skinner, Guest.
Replacements: Poluleuligaga for Gomarsall (78). Not Used: Brooker, Lambert, Robson, Mcmillan, Masson, Malone.
Bath: Abendanon, Stephenson, Cheeseman, Hape, Banahan, James, Claassens, Flatman, Dixon, Bell, Harrison, Short, Hooper, Faamatuainu, Browne.
Replacements: Berne for Cheeseman (78), Fuimaono-Sapolu for Hape (67), Bemand for Claassens (69), Hawkins for Dixon (73), Ion for Bell (63), Beattie for Harrison (63), Scaysbrook for Faamatuainu (73).
Att: 11,500
Ref: M Fox (RFU).