Pierre-Emerick Aubameyang fired Borussia Dortmund into the Champions League group stages with the winning goal in a dramatic 4-3 victory over Werder Bremen at Signal-Iduna Park.

The Gabonese striker netted his second goal of the game and his 31st of the season - making him the Bundesliga's top scorer on the final day of 2016-17 - from the penalty spot in the 89th minute of a game that swung from one end to the other throughout a scintillating afternoon of football in Dortmund.

After narrowly surviving relegation from the top flight last season, Bremen have enjoyed an excellent campaign under coach Alexander Nouri and Zlatko Junuzovic put them ahead after seven minutes when goal-line technology was used to establish that his close-range shot had crossed the line.

Shinji Kagawa inspired Dortmund's fightback and he provided the assist for Marco Reus to equalise before starting the move that saw Aubameyang score with a brilliant volley on the turn. Fin Bartels made it 2-2 inside the first minute of the second half and Max Kruse then put Bremen ahead, but Reus and Aubameyang both converted penalties in the final 15 minutes of the game to secure the win for Dortmund.

The defeat ended Bremen's hopes of qualifying for the Europa League, but they can take great satisfaction in having finished in the top half of the table – a feat that seemed beyond them at the start of the campaign.

Meanwhile, Dortmund can celebrate after avoiding the lottery of a Champions League qualifying play-off.

Bremen took the lead after just seven minutes when Florian Grillitsch played a low cross into the penalty area and Junuzovic slid in to poke it over the line. Roman Burki appeared to have saved the shot with his foot, but the referee was informed that the ball had crossed the line and awarded the goal.

Aubameyang had a claim for a penalty turned down after he went down under a challenge from Niklas Moisander after 22 minutes, and Matthias Ginter headed narrowly over the crossbar five minutes later as Dortmund probed for an equaliser with urgency in front of an increasingly worried home crowd.

The leveller came after 32 minutes, courtesy of the ever-industrious Kagawa's superb threaded pass through the middle of the Bremen defence to Reus, who steered a low shot just inside the post and beyond the reach of goalkeeper Felix Wiedwald.

Three minutes before half-time, Aubameyang gave Dortmund the lead with a picture-book goal, sending a sweetly struck volley on the turn into the roof of the net after Ousmane Dembele picked him out with a chipped pass.

Bremen refused to accept defeat without a fight in their final match of a very good season and they drew level within seconds of the second half.

Kruse sprinted almost the full length of Dortmund's half before turning Nuri Sahin on the by-line and crossing the ball to Bartels, who slammed it into the net with aplomb from inside the penalty area.

Bremen substitute Serge Gnabry put Kruse through on goal again five minutes later, but Marc Bartra's last-ditch challenge cleared the danger. Kruse would not be denied after 68 minutes, however, when he latched onto Junuzovic's pass and hit a shot that looped up off the shoulder of Burki and into the net to make it 3-2.

Dortmund's porous defence looked likely to be their undoing but they were handed a lifeline when Gnabry brought Reus down in the penalty area and the Germany international lashed the ensuing penalty into the net, beating Wiedwald to his left.

The final twist in a dazzling match came when substitute Philipp Bargfrede was judged to have brought down Christian Pulisic and Aubameyang stepped up to slot the penalty calmly to Wiedwald's right, sparking wild celebrations around the packed stadium.