Increasing the flaps does increase the drag, but not by that much initially. For the first stages of flaps you gain more by reducing required takeoff speed. If you would increase the flaps more and more, eventually the drag would become too much and you would lose takeoff distance again.
Flap setting has an affect on the wing’s lift coefficient and on the aerodynamic drag. Increasing flap angle increases the lift coefficient, and therefore reduces stalling speed and the required takeoff speed (the same lift will be created at smaller air speed due to greater lift coefficient). This reduces the takeoff distance. In the same time increased flap angle increases drag, reduces acceleration, and increases the takeoff distance.
The net effect is that takeoff distance will decrease with increase of flap angle initially, but above a certain flap angle the takeoff distance will increase again. An optimum takeoff setting can be determined for each type of aircraft and any deviation from this setting will give an increase in the takeoff distance.
The flap setting also affects the climb gradient. Increasing the flap angle increases the drag, and so reduces the climb gradient for a given aircraft mass. If there are obstacles to be considered in the takeoff flight path, the flap setting that gives the shortest takeoff distance may not give the required climb gradient for obstacle clearance.
Note that the climb gradient is also affected:
More flaps allows taking off from shorter runways, but reduces obstacle clearance capabilities.