Ugo Monye celebrated his Twickenham Stoop farewell with two tries but it was not enough to prevent Harlequins from slipping to a 27-26 defeat which guarantees Bath's presence in the Aviva Premiership play-offs.
Monye, who retires after next weekend's regular season finale at Newcastle, crossed in each half in an inspired evening that ended in the 68th minute when he departed to a standing ovation.
The 32-year-old former England and British and Irish Lions wing marked his 241st appearance for the club he has served since 2002 by lifting his try tally to 89 - and was named man of the match.
Quins were awarded a first-half penalty try and led 15-10 at half-time, but Bath dug deep in the final quarter of a pulsating match and George Ford finally separated the rivals with a 75th penalty.
Francois Louw, Horacio Agulla and Leroy Houston breached the whitewash for Bath and the title challengers - who host Gloucester next Saturday - are now just one win away from securing a home-final in the play-offs.
Six tries illuminated a thrilling spectacle with underdogs Quins overcoming the early loss of Nick Easter to concussion and George Robson, who departs for France in the summer, to injury to push Bath all the way.
Six minutes were on the clock when Quins surged ahead with a try started by one wing and finished with another.
Marland Yarde blasted the initial hole, Danny Care arrived in support and when the ball was recycled Nick Evans expertly sent a long pas to Monye, who strolled over to finish a move that was executed perfectly.
Problems were mounting for Quins, however, after Easter's evening was ended by concussion shortly before number eight Jack Clifford was sin-binned for illegally stopping a driving maul.
After he had returned quick hands from Peter Stringer, Ford and Kyle Eastmond sent number eight Houston on the rampage and on hand to finish the move was Louw.
Ford converted to compliment an earlier penalty before Evans landed three points as Bath led 10-8 at the half hour mark.
The home side went on the attack with Clifford skipping and powering his way through tackles and offloading to Chris Robshaw, whose muscular run swept his team into enemy territory.
Quins gathered en masse five metres out from the whitewash and the momentum carried them to the line with referee Greg Garner awarding a penalty try for collapsing.
David Wilson, making his comeback after three months out with a neck injury, could have seen a yellow card for hitting Monye with a right hand when the wing joined the maul and with Evans converting, Conor O'Shea's men led 15-10 at half time.
Two minutes into the second half Quins undid their good work by waving Agulla through a large gap that appeared after a line-out.
Only an outstanding tackle by Matt Banahan on George Lowe prevented a certain try after wonderful hands from Monye had set up his outside centre, but the 2012 champions could at least console themselves with two Evans penalties in quick succession.
The pendulum continued to swing in a compelling spectacle as tries were scored at both ends, Bath initiating a breathless period when Houston crashed over from close range following a speculative offload from Sam Burgess.
Quins' tenacity led to their response, hooker Dave Ward stabbing the ball free at a ruck into the path of Care and following a superb flicked on pass by Clifford, Monye was offered sight of the line and used his power and athleticism to finish the move.
The lead changed for a seventh and final time when Ford kicked his second penalty with five minutes left, finally breaking Quins' resistance.