Salzburg recovered from a terrible start to win in Bratislava and book their place in the knockout stages of the Europa League.
Needing to match Paris St Germain's result to go through, Ricardo Moniz's men conceded a brace to Milos Lacny inside the opening six minutes.
However, they crucially pulled one back quickly through a Jakob Jantsche penalty and, once Leonardo had levelled, a winner for the Austrians seemed inevitable.
It arrived in the second half from the boot of the unfortunate Marian Had, who scored an own goal to send Salzburg through with group winners Athletic Bilbao.
Just 150 seconds had passed when Lacny took in a pass from Igor Zofcak and found the bottom corner and, with Salzburg reeling, the home side scored again three minutes later.
Had was the creator this time, sending a raking pass out of his own half that put Lacny free and the striker duly finished beyond Alexander Walke.
Jantscher took aim from range for the shell-shocked visitors, but they were soon on a calmer footing as they pulled a goal back in the 19th minute.
Karim Guede handled in the area and Jantscher stepped up to put his side back in the game after a whirlwind opening.
The action did not stop there and, with just 23 minutes gone, Salzburg were level through Leonardo, the Brazilian finding space and firing home after the ball got loose in the box.
Remarkably it took 10 minutes for the next chance of note to present itself, Zofcak working Walke from the edge of the box, and even more of a surprise was the absence of any other opportunities before the break.
Salzburg's Stefan Hierlander tried his best to find a fifth goal after the restart, shooting over from outside the area, while Martin Dobrotka responded with a similar effort at the other end.
A fifth goal did duly arrive, although when it did it was not conventional, with Had putting thrown his own net in the 52nd minute, completing a thrilling comeback for the Austrian side.
Jiri Kladrubsky tried to make it 3-3 from the edge of the box but failed, while Petri Pasanen could have put Salzburg two goals clear with a header and Filip Sebo had a header of his own saved by Walke as the game continued to flow.
There would be no more goals, though, and Salzburg held on to progress in spite of PSG's win over Bilbao.