Sejad Salihovic converted two penalties in the final 13 minutes as Hoffenheim booked a spot in the relegation play-off with a thrilling win against 10-man Borussia Dortmund.
Robert Lewandowski gave Dortmund the lead after six minutes before Salihovic scored from the spot in the 77th and 82nd minutes.
On the second occasion he beat Dortmund midfielder Kevin Grosskreutz from 12 yards after goalkeeper Roman Weidenfeller was red-carded.
Dortmund had a goal four minutes into stoppage time disallowed which would have relegated Hoffenheim, but instead the strugglers will face Kaiserslautern in a two-leg play-off for a Bundesliga spot.
Dortmund finished the season with 66 points, taking second place, while Hoffenheim came in 16th with 31 points.
Ahead of next weekend's Champions League final against Bayern Munich, Dortmund made a flying start.
Jakub Blaszczykowski's shot was blocked by Hoffenheim goalkeeper Koen Casteels but it went straight to Lewandowski, who pounced on the loose ball from three yards to net his 24th goal of the season.
Blaszczykowski nearly made it 2-0 12 minutes later but Casteels was able to stop the Poland international's attempt.
Hoffenheim had a golden chance to equalise after 37 minutes with Roberto Firmino playing the ball forward to Salihovic, whose shot was stopped superbly by Weidenfeller.
Lewandowski missed a chance for a second goal two minutes later but headed wide to the left of the target.
Casteels made a fine stop from Grosskreutz in the 53rd minute, then deflected Ilkay Gundogan's shot off the goal frame a minute later and booted away a Blaszczykowski attempt in the 67th minute.
Hoffenheim equalised in the 77th minute from the spot as Mats Hummels fouled Kevin Volland in the box. Salihovic blasted a left-footed shot into the right corner.
The visitors were awarded another penalty three minutes later as Weidenfeller took down Sven Schipplock and was ordered off.
Dortmund had already made three substitutions, leaving Grosskreutz to take the gloves, and Salihovic blasted high into the centre of the goal.
Dortmund appeared to scupper Hoffenheim's survival hopes four minutes into stoppage time with Marcel Schmelzer netting from long range.
However Lewandowski was ruled to have gone actively after the ball from an offside position - sending Hoffenheim into celebrations when the goal was disallowed.