Nathan Earle's try snatched a 31-29 bonus-point victory for third-placed Harlequins with the last move of the game after Bath fought their way back from being 14-0 down in the first 11 minutes.
It was the second game in succession that Bath had thrown away victory in the dying seconds, after an equally agonising 27-26 loss at Northampton last Saturday.
Quins had monopolised possession and territory before taking the lead after eight minutes. Will Chudley escaped a penalty and possible yellow card when slapping at a pass but Quins eventually worked a close-range try from the scrum for Chris Robshaw.
No sooner had James Lang converted the try than he was teeing the ball up again in front of the posts after centre Joe Merchant profited from a loose Freddie Burns pass in midfield, hacked on by Alex Dombrandt.
Rhys Priestland eventually landed a penalty from 40 metres and Quins had to defend a succession of pick-and-drives before the first home try on 26 minutes.
Burns sliced through from deep and found Chudley in support and, although Quins regathered from his kick-ahead, the ball was spilled behind the ruck and Jonathan Joseph scored in the left corner.
Priestland failed to convertĀ but was successful with a penalty on the half-hour. Back came the visitors with a third try, cleverly fashioned for Dombrandt by Mike Brown and Marchant combining down the left.
On the stroke of half-time, a patient series of pick-and-drives ended with Nathan Catt touching down for Bath. Priestland's missed conversion left the score at 21-16 but he added his third penalty immediately after the break.
While Bath had picked up the pace, Quins' set piece was beginning to creak and Dave Attwood picked off a line-out which sparked Bath's best attack yet.
The second row, off to Bristol next season, then saved a certain try at the other end by holding up Dombrandt, allowing Priestland to clear their lines from the scrum.
But Quins had a narrow escape when the TMO ruled that Ruaridh McConnochie only just failed to ground a kick-ahead by Tom Homer.
A lapse in defence nearly let in James Horwill for a try for Quins but Bath struck just after the hour when Ross Batty combined with McConnochie who broke clear and, when tackled by Brown popped up as return pass for the replacement hooker to touch down.
Priestland's conversion put Bath two scores ahead, but not for long as Brown put Marchant over for a second try, unconverted but earning the bonus point.
There was still time for the TMO to rule against a try for Harlequins at a ruck on the Bath line but the home side had to defend the resulting scrum. A Kahn Fotuali'i turnover earned a relieving penalty but Quins stole the line-out and Earle finished off in the corner.