Background Trachyspermum ammi (L.) Sprague is used for treating gastrointestinal disorders. Several studies indicated gastric antiulcer activity of T. ammi extract, yet the effect of its essential oil has not been studied on. Objectives The present study evaluates chemical composition of T. ammi essential oil and anti-peptic ulcer effect of the essential oil as well as its three major components in ethanol induced-gastric ulcers in rats. Methods Primarily chemical composition of the essential oil was analyzed by gas chromatography-mass spectrometry (GC/MS). Rats received the essential oil (500, 250, 125, 62.5, 31.25 mg/kg), thymol (30, 100 mg/kg), para-cymene (100, 150 mg/kg) and gamma-terpinene (100, 150 mg/kg) using gavage tube along with ethanol 80%. Finally, dissected stomachs were assessed both macroscopically and microscopically to evaluate anti-ulcerative effect of the essential oil and the pure compounds. Moreover, molecular docking was utilized to explore the interactive behavior of the main components with active site residues of H + /K + ATPase. Results Analysis of the essential oil indicated that para-cymene (37.18%), gamma-terpinene (35.36%) and thymol (20.51%) are the main components. Administration of different doses of the essential oil noticeably diminished the number of peptic ulcers in a dose-dependent manner. Among the main components, thymol was more potent than para-cymene and gamma-terpinene. Administration of the essential oil (500 mg/kg) and thymol (100 mg/kg) observed maximum inhibition percentage (98.58% and 79.37%, respectively). Molecular docking study provides the evidence of thymol ability to inhibit H + /K + ATPase. Conclusions The findings revealed that T. ammi essential oil can be applied to treat gastric ulcer as a natural agent.