Northampton came back from 19 points down to secure a maximum haul against Bath and move into the top four.
Matt Proctor put Saints ahead before Max Clark and Will Muir replied for Bath, who led 17-7 at the break.
Juarno Augustus reduced the gap for the visitors but Ben Spencer's two tries in three minutes put Bath 31-12 up going into the final twenty minutes.
Alex Mitchell, Tommy Freeman and a penalty try levelled it up before Mikey Haywood won it at the death for Saints.
The thrilling finale saw Saints complete a fourth consecutive league win to move above Exeter, who play Saracens on Sunday, into fourth place, while Bath remain bottom of the Premiership despite their two bonus points.
Saints took the lead on 17 minutes after a long spell of pressure resulted in Mitchell sending a pass wide to Proctor for the New Zealander to dive over, with George Furbank adding the extras.
But Bath responded well and were soon on level terms when Clark chased a kick through before gathering and grounding over the line, with Danny Cipriani levelling it up from the tee.
Muir then crossed for his seventh try of the season and Cipriani's boot saw Bath into a ten-point lead at half-time.
Saints needed to begin the second half well and did just that, with Augustus powering over after a rolling maul had been stopped just short of the line to reduce the gap to five.
Spencer peeled away from a scrum in the Saints' 22 to score his first after 54 minutes, and two minutes later collected Muir's kick to grab a quickfire second and put Bath into a commanding 31-12 lead.
That was how it stayed until the 64th minute when Mitchell's darting score was quickly followed by Freeman's try to reduce the gap to seven.
A penalty try made it 31-31 as Saints piled on the pressure, and it was substitute hooker Haywood who touched down to seal victory in the final act of the game.
Bath head coach Neal Hatley:
"At 31-12, there shouldn't be a way back into the game for them. I am really frustrated. It wasn't good enough.
"We lost Josh Bayliss injured, then we had Jaco Coetzee in the sin-bin, so with 13 men, there is going to be a little bit of panic. There were some good bits, but that is the issue. The good bits are very good, but the drop-off is startling.
"When we've been good, we have been very good, and when we haven't been very good it has been very bad."
Saints director of rugby Chris Boyd:
"I thought we were terrible in the first 60 minutes. That was the worst we have played for a long time in that 60 minutes, but we managed to rally and step over at the end. I am very proud of the character shown by the guys at the end.
"We created very few chances, and those we created we butchered. It went out to 31-12, and at that stage I thought the season was over and we were just going to be playing for pride.
"The boys know that they have used their get out of jail card, and we won't get that opportunity against Harlequins on Friday or Saracens in the game after that. We know we have to be way better than that."
Bath: De Glanville; Rokoduguni, Joseph, Clark, Muir; Cipriani, Spencer; Morozov, Dunn, Stuart, Williams, Ewels (capt), Bayliss, Reid, Faletau
Replacements: Du Toit, Cordwell, Rae, Richards, Coetzee, Simpson, Bailey, Cokanasiga.
Yellow card: Coetzee
Northampton: Freeman; Sleightholme, Proctor, Hutchinson, Collins; Furbank, Mitchell; Waller, Matavesi, Hill, Coles, Ratuniyarawa, Ludlam (capt), Harrison, Augustus
Replacements: Haywood, Iyogun, Carey, Moon, Nansen, James, Grayson, Francis.