An injury-time goal from substitute Martyn Naylor was not enough, although it might have been had fellow substitute Ronnie Morgan's blistering 78th-minute drive not crashed again a post.
TNS began 3-2 ahead, but fell behind when Jean-Paul Nderki headed powerfully home Mihail Zizilev's 20th-minute corner.
The Saints held out until the break, but fell 2-0 down eight minutes into the second period when Denis Kacanovs headed in from another Zizilev corner.
The home side came close to scoring on several occasions before Naylor, whose brother Lee plays for Celtic, drove in a 22-yard free-kick off an upright.
Ventspils held on to clinch a second qualifying round clash against Red Bull Salzburg, managed by former Italy coach Giovanni Trapattoni.
Derry City's Champions League adventure ended in the Armenian capital of Yerevan as they faded after a bright start and slipped to a 2-0 defeat.
Sam Morrow and Kevin McHugh both had chances to give the Candystripes an early advantage in the second leg of their first qualifying round clash having drawn 0-0 at the Brandywell last week.
But their missed chances would cost them dear as the home side snatched the lead in the 28th minute when Arsen Avetisyan poked the ball home at the second attempt after a goalmouth scramble.
Derry had a penalty appeal turned down when Ciaran Martyn was hauled down in the box and Morrow fired across goal in the dying seconds of the first half.
Darren Kelly had the best chance to give Derry hope when he headed firmly against the Armenian crossbar early in the second half before a second Pyunik goal sealed their victory.
Gevorg Ghazarjan burst into the corner of the Derry box and fired a firm shot past Derry goalkeeper Patrick Jennings and into the net.
Former Charlton favourite Mathias Svensson struck to send Linfield tumbling out of the Champions League at the first qualifying stage in Sweden.
But the Northern Irishmen were left to rue a number of missed chances to force extra-time following their goalless first-leg draw at Windsor Park last week.
Noel Bailie brought a brilliant save out of Elfsborg goalkeeper Johan Wiland in the 69th minute as his side chased the equaliser in the second half.
Wiland also pulled off a fine save to save Mark Dickson's excellent volley while Peter Thompson flashed a header agonisingly wide one minute from time.
Linfield had been forced to chase the game after a dominant start by the Swedes capped by Svensson's somewhat freakish 33rd-minute opener.
Svensson - who also had spells in England with Portsmouth, Crystal Palace and Norwich, headed against the crossbar and the ball bounced down into the net off the shoulder of Alan Mannus.
Elfsborg could have extended their lead with Anders Svensson bringing a fine save out of Mannus, and Daniel Alexandersson also coming close in injury time.
- SPORTING LIFE -