Abstract Cholestatic liver disease is characterized by disturbances in the intestinal microbiota and excessive accumulation of toxic bile acids (BA) in the liver. Melatonin (MT) can improve liver diseases. However, the underlying mechanism remains unclear. This study aimed to explore the mechanism of MT on hepatic BA synthesis, liver injury, and fibrosis in 3,5‐diethoxycarbonyl‐1,4‐dihydrocollidine (DDC)‐fed and Mdr2 –/– mice. MT significantly improved hepatic injury and fibrosis with a significant decrease in hepatic BA accumulation in DDC‐fed and Mdr2 –/– mice. MT reprogramed gut microbiota and augmented fecal bile salt hydrolase activity, which was related to increasing intestinal BA deconjugation and fecal BA excretion in both DDC‐fed and Mdr2 –/– mice. MT significantly activated the intestinal farnesoid X receptor (FXR)/fibroblast growth factor 15 (FGF‐15) axis and subsequently inhibited hepatic BA synthesis in DDC‐fed and Mdr2 –/– mice. MT failed to improve DDC‐induced liver fibrosis and BA synthesis in antibiotic‐treated mice. Furthermore, MT provided protection against DDC‐induced liver injury and fibrosis in fecal microbiota transplantation mice. MT did not decrease liver injury and fibrosis in DDC‐fed intestinal epithelial cell‐specific FXR knockout mice, suggesting that the intestinal FXR mediated the anti‐fibrosis effect of MT. In conclusion, MT ameliorates cholestatic liver diseases by remodeling gut microbiota and activating intestinal FXR/FGF‐15 axis‐mediated inhibition of hepatic BA synthesis and promotion of BA excretion in mice.