Melatonin has important immuno-regulatory effects in inflammatory disorders but its specific role in Helicobacter pylori induced gastritis remains unclear. The aim of our study was to analyze the activity of melatonin against H. pylori induced gastritis in vivo, and explore the underlying mechanisms. The H. pylori infected mice showed extensive inflammatory cell infiltration in the gastric mucosa and submucosa, along with significantly reduced spleen and thymus weight. However, 2 and 6 weeks of treatment with 25 and 50 mg/kg melatonin restored the thymus weights relative to that of the untreated mice. TLR2 was upregulated in the gastric mucosa of the infected mice, which was restored to normal levels after 2 and 6 weeks of melatonin treatment. In contrast, TLR4 levels were similar between the treated and untreated mice. Furthermore, melatonin treatment restored spleen Foxp3 and serum TGF-β1 levels that were respectively increased and decreased in the infected mice. H. pylori infected mice also showed a decrease in the serum levels of IL-2, IL-6, IL-10, IL-17, IFN-γ and TFN-α following 2 and 6 weeks of melatonin treatment compared to the untreated mice. Melatonin treatment also resulted in decreased CD4+CD25+Foxp3+ Treg cell count in the spleen. The expression of TLR2, MyD88, p-ERK, p-p38, p65, p50 and Foxp3 in the gastric tissues were lower in the untreated mice compared to mice treated with melatonin for 2 weeks. However, the expression levels evened out after 6 weeks of treatment. Taken together, melatonin alleviates H. pylori induced gastritis by regulating TGF-β1 and Foxp3 expression via the TLR2 and TLR4 pathways.