Right wing Tom Williams, hooker Rob Buchanan and centre Tom Casson scored tries for Quins, with fly-half Nick Evans landing two conversions and a penalty.
Saints, narrowly denied after rallying from 22-5 down, replied with tries from left wing Jamie Elliott, hooker Dylan Hartley and lock Christian Day, on his 100th appearance for the club.
Fly-half Stephen Myler converted two of the tries but had earlier missed two penalties and a conversion.
Harlequins leapfrogged Northampton to finish third on a day when the two sides, already assured of their play-off places before the final day of the regular season, served up a feast of attacking rugby.
Evans kicked Harlequins into an 11th-minute lead with a simple 20-metre penalty after Saints strayed offside in front of their own posts.
Quins increased their lead 11 minutes later when a finely judged kick down the right touchline by Danny Care bounced perfectly for Williams - and cruelly for Northampton full-back Ben Foden - leaving the winger with a clear sprint to the line for a try converted by Evans.
The visitors responded with a sweeping attack which resulted in centre Luther Burrell timing his pass to perfection to send right wing Ken Pisi through a gap in the home defence.
But referee Tim Wigglesworth consulted television match official David Grashoff before the try was ruled out because a would-be Harlequins tackler had been obstructed in the build-up.
Northampton were not denied for long as Foden bounced back from his earlier misfortune to provide a kick behind the Quins' defence which enabled Elliott to collect and race clear for the line. Myler's conversion from the left touchline drifted wide.
The fast-moving game continued to swing from end to end and Mike Brown's 40-metre kick down the middle from his own half had alarm bells ringing for the Saints.
But Elliott cleared the danger, reacting quickly to reach the loose ball just ahead of the Harlequins full-back.
Foden's fluctuating fortunes took a turn for the worse when Evans read his pass perfectly to grab an interception but, as he kicked ahead down the line, the fly-half was crash-tackled into touch by the covering Courtney Lawes before he could begin the chase.
Quins scored their second try less than two minutes into the second half when flanker Luke Wallace flicked up a pass as he was tackled for Buchanan to charge through and ground the ball on the foot of the post.
Referee Wigglesworth once again referred to the TMO before allowing the try, converted by Evans.
The fly-half had a chance to increase his side's 17-5 lead three minutes later but put his long-range penalty wide.Quins struck again in the 51st minute following a tremendous break through the middle by Care.
He was stopped just short but number eight Nick Easter moved the ball on before George Lowe supplied the final pass for his centre partner Casson to touch down.
Northampton launched a fightback which produced 14 points in a seven minute spell.
A superbly executed rolling maul enabled Hartley to celebrate his British and Irish Lions selection with a try before a great passing move along the line ended with Day crossing for their third try.
Myler converted both to set up a grandstand finish but Quins held on to their slender lead.