Background: Cognitive dysfunction is a critical complication of diabetes mellitus, and there are still no clinically approved drugs. Zi Shen Wan Fang (ZSWF) is an optimized prescription composed of Anemarrhenae Rhizoma, Phellodendri Chinensis Cortex, and Cistanches Herba. The purpose of this study is to investigate the effect of ZSWF on DCI and explore its mechanism from the perspective of maintaining intestinal microbial homeostasis in order to find an effective prescription for treating DCI.Methods: The diabetes model was established by a high-fat diet combined with intraperitoneal injections of streptozotocin (STZ, 120 mg/kg) and the DCI model was screened by Morris water maze (MWM) after 8 weeks of continuous hyperglycemic stimulation. The DCI mice were randomly divided into the model group (DCI), the low- and high-ZSWF–dose groups (9.63 g/kg, 18.72 g/kg), the mixed antibiotic group (ABs), and the ZSWF combined with mixed antibiotic group (ZSWF + ABs). ZSWF was administered orally once a day for 8 weeks. Then, cognitive function was assessed using MWM, neuroinflammation and systemic inflammation were analyzed by enzyme-linked immunosorbent assay kits, intestinal barrier integrity was assessed by hematoxylin-eosin (HE) staining and Western blot and high performance liquid chromatography tandem mass spectrometry (UPLC-MS/MS). Furthermore, the alteration to intestinal flora was monitored by 16S rDNA sequencing.Results: ZSWF restored cognitive function in DCI mice and reduced levels of proinflammatory cytokines such as IL-1β, IL-6, and TNF-α. Moreover, ZSWF protected the integrity of the intestinal barrier by increasing intestinal ZO-1 and occludin protein expression and decreasing urinary lactulose to mannitol ratio. In addition, ZSWF reshaped the imbalanced gut microbiota in DCI mice by reversing the abundance changes of a wide range of intestinal bacteria at the phyla and genus levels. In contrast, removing gut microbiota with antibiotics partially eliminated the effects of ZSWF on improving cognitive function and reducing inflammation, confirming the essential role of gut microbiota in the improvement of DCI by ZSWF.Conclusion: ZSWF can reverse cognitive impairment in DCI mice by remolding the structure of destructed gut microbiota community, which is a potential Chinese medicine prescription for DCI treatment.