Cognitive decline is one of the complications of type 2 diabetes (T2D). Intermittent fasting (IF) is a promising dietary intervention for alleviating T2D symptoms, but its protective effect on diabetes-driven cognitive dysfunction remains elusive. Here, we find that a 28-day IF regimen for diabetic mice improves behavioral impairment via a microbiota-metabolites-brain axis: IF enhances mitochondrial biogenesis and energy metabolism gene expression in hippocampus, re-structures the gut microbiota, and improves microbial metabolites that are related to cognitive function. Moreover, strong connections are observed between IF affected genes, microbiota and metabolites, as assessed by integrative modelling. Removing gut microbiota with antibiotics partly abolishes the neuroprotective effects of IF. Administration of 3-indolepropionic acid, serotonin, short chain fatty acids or tauroursodeoxycholic acid shows a similar effect to IF in terms of improving cognitive function. Together, our study purports the microbiota-metabolites-brain axis as a mechanism that can enable therapeutic strategies against metabolism-implicated cognitive pathophysiologies.