Organisms as simple as bacteria can engage in complex collective actions, such as group motility and fruiting body formation. Some of these actions involve a division of labor, where phenotypically specialized clonal subpopulations, or genetically distinct lineages cooperate with each other by performing complementary tasks. Here, we combine experimental and computational approaches to investigate potential benefits arising from division of labor during biofilm matrix production. We show that both phenotypic and genetic strategies for a division of labor can promote collective biofilm formation in the soil bacterium Bacillus subtilis. In this species, biofilm matrix consists of two major components; EPS and TasA. We observed that clonal groups of B. subtilis phenotypically segregate into three subpopulations composed of matrix non-producers, EPS-producers, and generalists, which produce both EPS and TasA. This incomplete phenotypic specialization was outperformed by a genetic division of labor, where two mutants, engineered as specialists, complemented each other by exchanging EPS and TasA. The relative fitness of the two mutants displayed a negative frequency dependence both in vitro and on plant roots, with strain frequency reaching a stable equilibrium at 30% TasA-producers, corresponding exactly to the population composition where group productivity is maximized. Using individual-based modelling, we show that asymmetries in strain ratio can arise due to differences in the relative benefits that matrix compounds generate for the collective; and that genetic division of labor can be favored when it breaks metabolic constraints associated with the simultaneous production of two matrix components.