Harlequins produced a brilliant performance to beat Bath and, in the process, pip them to a spot in the Premiership play-offs.
Quins had started the day one place below their opponents, but victory lifts them into the top four.
The hosts led 10-7 at the break, with Danny Care's fine pass sending Mike Brown over before George Ford jinked his way through to score for Bath.
And Nick Evans kicked three penalties in the second half to secure the win.
But Ford, who struck three penalties of his own after the interval to keep the visitors within three points, might have snatched a draw with the last play of the game had his long-range drop-goal attempt gone over.
His miss means agony for Mike Ford's side, who had spent the vast majority of the season camped in the top four only to lose out on the final day.
But Harlequins, Premiership champions in 2012, have now reached the end-of-season play-offs for three seasons running and will face table-toppers Saracens at Allianz Park on Saturday, 17 May in the semi-finals.
After a tense, error-strewn opening period Quins went ahead on the 15-minute mark, with a try created and finished by two of their England stars.
Following a spell of sustained pressure Care collected the ball centrally, 10 metres from the visitors' line, and sent a brilliant, teasing pass out to Brown, who darted over in the corner, with Evans adding the extras.
But Ford, who scored all 19 of Bath's points in their draw against Northampton last week, provided another moment of brilliance to level matters at The Stoop, skipping his way between three Quins defenders to go over beneath the posts and then sending over a simple conversion.
Nick Easter, making a club record-equalling 232nd appearance for Harlequins, looked to have the home side back in front soon after with a try of his own, only for his effort to be ruled out by the television match official because Care's offload went forward.
But Evans ensured they would go into the interval ahead, slotting a penalty four minutes before half-time.
Bath drew level through Ford's kick soon after the break, before Matt Banahan was sin-binned for disrupting a Quins attack from an offside position and two further Evans penalties during the winger's stint off the field gave the hosts a six-point advantage.
However, Ford struck two penalties for the visitors, either side of another Evans kick, to keep Bath in touch heading into the closing stages.
But, with the clock having passed 80 minutes, the 21-year-old England fly-half failed with an ambitious drop-goal attempt to send the home fans into delirium and Conor O'Shea's team into the play-offs.
Harlequins director of rugby Conor O'Shea:
"There was a huge amount of tension and I'm incredibly proud of the belief within that group.
"We've won 13 out of our last 17, we've won our last five in a row. We've had to scrape wins, but mental strength is a huge thing.
"It doesn't happen by accident, you don't just turn one-point wins and two-point wins your way, it comes from a huge belief in each other.
"We'll give it a good crack against Saracens, we'll play our way and if it isn't good enough I won't change the way I feel about what this group has done and what it will go on to do.
"We'll be completely written off, I'm pretty comfortable with that - you can write this team off any day."
Bath head coach Mike Ford:
"I've never been as disappointed in my life since being involved in the game.
"I don't want any sympathy, we should have got to the top four today and we didn't. Is it because the team is young? No, the team is good enough to do it, we should have done it.
"Maybe in July I might come round a little bit and think it's been a good season.
"The main thing for us is we're only a year into this group and the potential is enormous, absolutely enormous."
Harlequins: Brown; Williams, Molenaar, Turner-Hall, Monye; Evans, Care; Marler, Ward, Sinckler; Matthews, Robson; Fa'asavalu, Robshaw (capt), Easter.
Replacements: Smith for Monye (48), Guest for Fa'asavalu (53). Not Used: Buchanan, Lambert, Collier, Kennedy, Dickson, Botica.
Bath: Abendanon; Watson, Joseph, Devoto, Banahan; Ford, Young; James; Guinazu, Wilson; Hooper (capt), Attwood; Fearns, Mercer, Houston.
Replacements: Rokoduguni for Abendanon (48), Stringer for Young (53), Dunn for Guinazu (14), Perenise for Wilson (53), Day for Hooper (61), Faosiliva for Mercer (58). Not Used: Orlandi, Henson.
Sin Bin: Banahan (47).
Referee: Wayne Barnes