Allicin has antimicrobial properties and can therefore protect garlic from fungal and bacterial infections and is coded for in the DNA of the garlic by an allele.
The presence could have been the result of widespread microbial infections that infect garlic plants in the past. The change in the environment would apply a selection pressure onto the garlic plant population, and the plants that had the advantageous allele for Allicin would survive, making them more likely to reproduce and pass on the allele to the future garlic plant generations. These plants would also reproduce, and overtime, the allele frequency for the advantageous Allicin allele would increase, until it was present across the entire garlic plant population.