Ross County came from behind to beat Motherwell 2-1 and send Kilmarnock into the Scottish Premiership play-offs.
County needed at least a point to guarantee their safety and fell behind in the seventh minute at Fir Park when Sam Foley netted from close range.
Killie had to win against Hamilton to have any hope of avoiding a two-legged clash with Dundee and they opened the scoring in the ninth minute through Mitch Pinnock.
Pinnock added his second just before half-time to put County up against it but Iain Vigurs levelled with a brilliant curling finish within four minutes and Michael Gardyne put them ahead 20 minutes into the half.
Pinnock missed a penalty to deny him a hat-trick but Killie's 2-0 win at Hamilton only confirmed the home side's relegation – they had needed a nine-goal victory to get off the bottom.
Tommy Wright's side now face a two-legged clash with Dundee for a place in next season's top flight, with the first game scheduled for Thursday at Dens Park.
Motherwell's defeat had consequences for them – they had needed at least a point to secure seventh.
St Mirren's goalless draw with Dundee United was enough for them to leapfrog Well and secure their highest league finish since 1989.