The Brazilian's 25-yard left-foot shot swerved in off the underside of the bar to send bottom club Darmstadt to a seventh straight defeat.
Bayern laboured for much of the game against spirited opponents.
Peter Niemeyer should have equalised, heading straight at Manuel Neuer, who had parried Jerome Gondorf's free-kick.
Carlo Ancelotti's side regained first place from Leipzig, who had moved three points clear by beating Hertha Berlin 2-0 on Saturday.
Leipzig have supplanted Borussia Dortmund as Bayern's biggest Bundesliga title rivals this season, and Wednesday's meeting between the two side in Munich is keenly anticipated.
There was a sense that Ancelotti and his players may have had one eye on that fixture, as they were far from at their best against Darmstadt.
Bayern captain Philipp Lahm, who was listed on the team sheet, suffered a problem during the warm-up and was not risked - perhaps with Wednesday in mind - as Rafinha was drafted in to replace him.
There was little fluency about Bayern's play, and Robert Lewandowski - who signed a contract extension lasting until 2021 on Tuesday - did not look like continuing the run that had brought him five goals in his previous three matches.
The closest that the Poland striker came to a goal was a free-kick floated just over the bar towards the end of the first half, on a day when Bayern rarely threatened other than from set-pieces.
There was a degree of encouragement in defeat for Darmstadt, who have struggled to recover from the departure of coach Dirk Schuster for Augsburg during the summer.
Norbert Meier, Schuster's replacement, was sacked on 5 December, but interim coach Ramon Berndroth coaxed an energetic performance from his players, and they would have gained a valuable point but for Costa's unstoppable shot.
Match ends, SV Darmstadt 98 0, FC Bayern München 1.
Second Half ends, SV Darmstadt 98 0, FC Bayern München 1.
Joshua Kimmich (FC Bayern München) wins a free kick in the defensive half.
Foul by Victor Obinna (SV Darmstadt 98).
Javi Martínez (FC Bayern München) wins a free kick in the defensive half.
Foul by Victor Obinna (SV Darmstadt 98).
Substitution, FC Bayern München. Renato Sanches replaces Douglas Costa.
Substitution, SV Darmstadt 98. Victor Obinna replaces Sven Schipplock.
Foul by Franck Ribéry (FC Bayern München).
Florian Jungwirth (SV Darmstadt 98) wins a free kick in the defensive half.
Attempt missed. Antonio-Mirko Colak (SV Darmstadt 98) header from the centre of the box misses to the left. Assisted by Marcel Heller with a cross.
Hand ball by Franck Ribéry (FC Bayern München).
Franck Ribéry (FC Bayern München) wins a free kick in the defensive half.
Dangerous play by Sandro Sirigu (SV Darmstadt 98).
Substitution, FC Bayern München. Joshua Kimmich replaces Thomas Müller.
Substitution, SV Darmstadt 98. Antonio-Mirko Colak replaces Peter Niemeyer.
Rafinha (FC Bayern München) wins a free kick in the defensive half.
Foul by Marcel Heller (SV Darmstadt 98).
Attempt saved. Peter Niemeyer (SV Darmstadt 98) header from the centre of the box is saved in the bottom right corner.
Attempt saved. Jérôme Gondorf (SV Darmstadt 98) right footed shot from outside the box is saved in the bottom right corner.
Xabi Alonso (FC Bayern München) is shown the yellow card for a bad foul.
Mats Hummels (FC Bayern München) is shown the yellow card for a bad foul.
Foul by Mats Hummels (FC Bayern München).
Mario Vrancic (SV Darmstadt 98) wins a free kick in the attacking half.
Javi Martínez (FC Bayern München) wins a free kick in the defensive half.
Foul by Sven Schipplock (SV Darmstadt 98).
Attempt missed. Thomas Müller (FC Bayern München) header from the centre of the box misses to the right. Assisted by David Alaba with a cross.
Goal!  SV Darmstadt 98 0, FC Bayern München 1. Douglas Costa (FC Bayern München) left footed shot from outside the box to the top left corner. Assisted by Thiago Alcántara following a set piece situation.
Rafinha (FC Bayern München) wins a free kick in the attacking half.
Foul by Fabian Holland (SV Darmstadt 98).
Substitution, FC Bayern München. Franck Ribéry replaces Arturo Vidal.
Delay over. They are ready to continue.
Delay in match Jérôme Gondorf (SV Darmstadt 98) because of an injury.
Attempt blocked. Douglas Costa (FC Bayern München) right footed shot from outside the box is blocked. Assisted by Thiago Alcántara.
Mats Hummels (FC Bayern München) wins a free kick in the defensive half.
Foul by Sven Schipplock (SV Darmstadt 98).
Foul by Arturo Vidal (FC Bayern München).
Fabian Holland (SV Darmstadt 98) wins a free kick in the defensive half.
Substitution, SV Darmstadt 98. Florian Jungwirth replaces Jan Rosenthal.
Attempt blocked. Robert Lewandowski (FC Bayern München) header from the centre of the box is blocked. Assisted by Douglas Costa with a cross.
Douglas Costa's spectacular winner took Bayern Munich top of the Bundesliga on goal difference before Wednesday's meeting with title rivals RB Leipzig.