Erling Haaland scored a penalty on his final appearance for Borussia Dortmund in a 2-1 win over Hertha Berlin, who face a play-off to stay in the Bundesliga.
The visitors took the lead at Signal Iduna Park through a spot-kick from Ishak Belfodil in the 18th minute.
Haaland – set to head to the Premier League next season after Manchester City triggered his £51.1million release clause – had Dortmund level from a penalty with 20 minutes left.
Jude Bellingham then played in Youssoufa Moukoko to slot home from a tight angle in the 84th minute – which coupled with a late winner for Stuttgart sent Felix Magath’s side into the relegation play-offs.
Stuttgart had started the day in 16th place and looked set to stay there after Anthony Modeste had headed in equaliser for Cologne on the hour to cancel out Sasa Kalajdzic’s early opener.
However, in stoppage time, Wataru Endo nodded in a from a corner to spark pandemonium around the Mercedes-Benz Arena.
RB Leipzig secured fourth spot and with it a place in the Champions League after a late goal from defender Willi Orban salvaged a 1-1 draw at relegated Arminia Bielefeld.
Janni Serra had bundled the home side in front with 20 minutes left, which had looked enough to sign off from the top flight with a win – only for Orban to nod home a stoppage-time equaliser from a free-kick.
Robert Lewandowski, who has been linked with a summer move away, took his Bundesliga goal tally to 35 as champions Bayern Munich ended the campaign with a 2-2 draw at Wolfsburg – where they had been 2-0 up at half-time.
Union Berlin beat Bochum 3-2, with Taiwo Awoniyi scoring twice, to finish in fifth place, a point behind Leipzig, and go into the Europa League.
A last-minute goal from Exequiel Palacios saw third-placed Bayer Leverkusen end the campaign with a 2-1 home win over Freiburg, who finish sixth.
Marcus Ingvartsen scored twice as Mainz held Europa League finalists Eintracht Frankfurt to a 2-2 draw, while bottom club Greuther Furth went down 2-1 at Augsburg, where Michael Gregoritsch struck a late winner.
Jonas Hofmann grabbed a brace to help Borussia Monchengladbach come from behind to sweep aside Hoffenheim 5-1.