As pectin is widely used as a food and feed additive due to its tremendous prebiotic potentials for gut health. Yet, the underlying mechanisms associated with its protective effect remain unclear. Twenty-four piglets (Yorkshire × Landrace, 6.77 ± 0.92 kg) were randomly divided into three groups with eight replicates per treatment: (1) Control group (CON), (2) Lipopolysaccharide-challenged group (LPS), (3) Pectin-LPS group (PECL). Piglets were administrated with LPS or saline on d14 and 21 of the experiment. Piglets in each group were fed with corn-soybean meal diets containing 5% citrus pectin or 5% microcrystalline cellulose. Our result showed that pectin alleviated the morphological damage features by restoring the goblet numbers which the pig induced by LPS in the cecum. Besides, compared with the LPS group, pectin supplementation elevated the mRNA expression of tight junction protein [Claudin-1, Claudin-4, and zonula occludens-1 (ZO-1)], mucin (Muc-2), and anti-inflammatory cytokines [interleukin 10 (IL-10), and IL-22]. Whereas pectin downregulated the expression of proinflammatory cytokines (IL-1β, IL-6, IL-18), tumor necrosis factor-&alpha (TNF-α), and NF-κB. What is more, pectin supplementation also significantly increased the abundance of beneficial bacteria (Lactobacillus, Clostridium_sensu_stricto_1, Blautia, and Subdoligranulum), and significantly reduced the abundance of harmful bacteria, such as Streptococcus. Additionally, pectin restored the amount of short-chain fatty acids (SCFAs) after being decreased by LPS (mainly Acetic acid, Propionic acid, and Butyric acid) to alleviate gut injury and improve gut immunity via activating relative receptors (GPR43, GPR109, AhR). Mantel test and correlation analysis also revealed associations between intestinal microbiota and intestinal morphology, and intestinal inflammation in piglets. Taken together, dietary pectin supplementation enhances the gut barrier and improves immunity to ameliorate LPS-induced injury by optimizing gut microbiota and their metabolites.