Werder, deep in relegation trouble in the Bundesliga after losing eight of their last 10 league games, shocked the visitors with two goals in the first half.
New signing Davie Selke scored from close range after 16 minutes and Leonardo Bittencourt made it 2-0 with a thundering volley on the half hour.
The second-half introduction of in-form striker Erling Haaland, who scored seven goals in his first three games for the club, instantly gave the visitors hope and the Norwegian teenager cut the deficit in the 67th.
Haaland on target as Dortmund brush aside Union Berlin
Milot Rashica restored the hosts' two-goal cushion three minutes later but another Dortmund teenager, 18-year-old Giovanni Reyna's sensational curled shot into the top corner set up a dramatic finale.
Despite a string of late Dortmund chances and a golden opportunity for Haaland with a header, Werder managed to hang on for victory.
Eintracht Frankfurt also advanced after Filip Kostic scored twice to help them beat last year's finalists RB Leipzig 3-1 and earn their spot in the competition's quarter-finals.
Holders Bayern Munich take on Hoffenheim for a spot in the next round on Wednesday.
Beckham's Inter Miami pursue Real Madrid and PSG stars - Euro Papers
Man Utd eye ambitious swoop for Dortmund duo - Paper Round
Opinion: Bellingham on course to becoming next £100m Englishman