Bath fly-half Stephen Donald kicked all his side's points to help inflict a third defeat of the season on Premiership leaders Harlequins.
Two penalties from Nick Evans twice put Harlequins in front, but fellow New Zealander Donald landed three kicks as Bath led 9-6 at half-time.
Donald took his tally to 21 points with four successful kicks after the break.
But Evans could only manage four from his five attempts as Bath held on to move up to fifth place in the table.
It was a tough evening for the Premiership's top two as second-placed Saracens were
beaten at Worcester.
The first points of the night went Harlequins' way when Evans kicked successfully from 26 metres after good early pressure from the visitors.
Donald missed his first attempt at goal soon after, but was more accurate in the 16th minute to level the game.
Bath began to create pressure of their own and Tom Williams' pass was intercepted by Matt Banahan, but he chose to pass rather than head for the line and the move broke down.
Evans kicked his second penalty to give Harlequins a 6-3 lead, before two quick penalties from Donald just before the break saw the hosts to a three-point lead at the interval.
Donald put the first points on the board in the second half to extend Bath's lead to 12-6, but Evans missed a simple attempt to close the gap - his first wayward kick of the match.
Karl Dickson came close to scoring the game's first try but was held up on the line, before Evans landed two successful penalties to draw Harlequins level.
Donald and Evans then traded penalties in a matter of minutes to see the game level at 15-15.
Bath thought they scored a try but the TMO was unable to confirm whether Banahan had grounded the ball on the line when breaking through the middle of a ruck.
Donald kept his calm from the resulting penalty to give the home side the advantage again and then landed another five metres in from touch to extend his side's lead to six points.
Evans's sixth penalty ensured Harlequins went into the final five minutes with a chance, but Bath held firm to secure their fifth Premiership win of the season.
VIEW FROM THE DRESSING ROOM
Bath first-team coach Toby Booth:
"This is the rebirth of the new Bath and what we are trying to get to.
"That should be the standard every week. Part of the evolution of this team is understanding that. We're down at Welford Road next week against Leicester, the side that everyone aspires to be. We are going to be tested even further.
"Everyone knows what a good side Leicester are - they're an outstanding side, well coached, well drilled. We are going to have to produce things like we did tonight to give ourselves the best opportunity."
Harlequins director of rugby Conor O'Shea:
"It's going to be a roller coaster of a season. We just have to stick to our guns the whole way through. The younger fellows who played tonight will have learned a lot.
"The effort, the attitude of the boys was fantastic, right to the bitter end. But we have to look at ourselves. Every time we got field position and got points, we never dealt with the kick-off.
"And the number of penalties they got through our inaccuracy, clearing our lines, was just not good enough. But it's a 22-game battle. We got a point, should have been more, but all credit to them."
Abendanon; Eastmond, Rokoduguni, Banahan, Biggs; Donald, Claassens; Beech, Mears, Perenise; Hooper (capt), Attwood; Ovens, Mercer, Taylor.
Day for Attwood (50), Cotter for Beech (77), Sharman for Mears (77).
Palma-Newport, Caldwell, Skuse, McMillan, Cuthbert.
Williams; Monye, Hopper, Turner-Hall, Smith; Evans, Dickson; Lambert, Gray, Collier; Kohn, Robson; Guest, Wallace, Easter (capt).
Stegman for Monye (49), Trayfoot for Guest (66), Buchanan for Gray (69), Sinckler for Collier (71).
Marfo, Browne, Burns, Clegg.