Development of mating preference is considered to be an early event in speciation. In this study, mating preference was achieved by dividing a population of Drosophila melanogaster and rearing one part on a molasses medium and the other on a starch medium. When the isolated populations were mixed, "molasses flies" preferred to mate with other molasses flies and "starch flies" preferred to mate with other starch flies. The mating preference appeared after only one generation and was maintained for at least 37 generations. Antibiotic treatment abolished mating preference, suggesting that the fly microbiota was responsible for the phenomenon. This was confirmed by infection experiments with microbiota obtained from the fly media (before antibiotic treatment) as well as with a mixed culture of Lactobacillus species and a pure culture of Lactobacillus plantarum isolated from starch flies. Analytical data suggest that symbiotic bacteria can influence mating preference by changing the levels of cuticular hydrocarbon sex pheromones. The results are discussed within the framework of the hologenome theory of evolution.