Scotland scored an injury-time winner to beat Poland in Warsaw and avoid automatic relegation from League A in the Nations League.
John McGinn and Andy Robertson were both on target to secure a 2-1 victory in Monday’s final group stage clash.
Both sides had plenty of chances to win the game but it was Scotland who took all three points to go ahead of Poland in the group.
However, a draw for Croatia against Portugal in Split means Steve Clarke’s men narrowly miss out on a place in the knockout stages.
They will now face a playoff with a League B runner-up to stay in the top tier of the competition.
McGinn opened the scoring early on to give Scotland the lead before Kamil Piatkowski equalised for the hosts midway through the second-half.
It was the Aston Villa star’s 20th international goal that puts him ahead of Ally McCoist in fifth of the all-time rankings.
Robertson headed home in the closing stages to seal the win on his 80th Scotland appearance.
Kenny Dalglish is now the only outfield player with more caps for the national team as the captain equals Darren Fletcher’s haul.
They got off to the best possible start by taking the lead when the game was just two minutes old.
Ben Doak collected a ball from Billy Gilmour before bursting into the box and cutting it back to McGinn who glided it into the net from the edge of the area to make it 1-0.
Poland had a good spell of possession after that, but Scotland also looked dangerous on the break and Doak was put through one on one with goalkeeper Łukasz Skorupski by Scott McTominay before having his effort well saved.
McTominay then came close to doubling the lead just after the half hour mark when he was put through by Doak before knocking it around the defender and trying his luck from just outside the box with Skorupski making another good save.
Craig Gordon was called into action soon after and he did well to keep out Adam Buksa.
Just as that was happening word came through from Split that Portugal had taken the lead against Croatia to give Scotland’s chances of reaching the knockout stages a major boost.
Billy Gilmour had a shot come off the crossbar before McTominay came close again when his strike was touched on to the post by the Poland keeper.
Robertson came close from the rebound but Poland managed to survive again.
At half-time Steve Clarke’s men were just one-goal away, either for them or against Croatia, from taking second spot in the group and reaching the knockout stages of a major international tournament for the first time.
Poland came out the traps early in the second-half and Scotland had John Souttar to thank for keeping them ahead when he made a last-ditch block to deny Kaminski whose shot had beat Gordon.
Doak and McTominay linked up again before the Napoli star sent an effort well over the crossbar.
Poland got themselves level soon after when right-back Piatkowski smashed the ball into Gordon’s top-corner to make it 1-1.
The was flowing end-to-end with chances coming thick and fast at both ends, and Lyndon Dykes coming closest when his header was brilliantly clawed away from danger by Skorupski who was proving to be a thorn in Scotland’s side with his performance.
Clarke then rang the changes with Ryan Christie and Lawrence Shankland replacing Doak and Dykes.
Back in Split, Man City defender Josep Gvardiol equalised for Croatia against Portugal as Scotland’s hopes of reaching the quarterfinals started to dwindle.
And just as though it looked like it was finishing 1-1, Robertson popped up with a header to make it 2-1 and win the game for Scotland.
The two-legged playoffs will take place next March with the second-leg taking place at Hampden.
Follow STV News on WhatsApp
Scan the QR code on your mobile device for all the latest news from around the country