An increasing number of elderly individuals are experiencing postoperative cognitive dysfunction (POCD) problems after undergoing hip replacement surgery, with gut microbiota metabolites playing a role in its pathogenesis. Among these, the specific effects of trimethylamine N-oxide (TMAO) on POCD are still unclear. This study aimed to explore the role of TMAO on cognitive dysfunction and underlying mechanisms in mice. The POCD model was created through femoral fracture surgery in elderly mice, followed by cognitive function assessments using the Morris Water Maze and Novel Object Recognition tests. The gut microbiota depletion and fecal microbiota transplantation were performed to examine the relationship between TMAO levels and cognitive outcomes. The effects of TMAO treatment on cognitive dysfunction, microglial activation, and inflammatory cytokine levels in the brain were also evaluated, with additional assessment of the role of microglial ablation in reducing TMAO-induced cognitive impairment. Elevated TMAO levels were found to be associated with cognitive decline in mice following femoral fracture surgery, with gut microbiota depletion mitigating both TMAO elevation and cognitive dysfunction. In contrast, fecal microbiota transplantation from postoperative mice resulted in accelerated cognitive dysfunction and TMAO accumulation in germ-free mice. Furthermore, TMAO treatment worsened cognitive deficits, neuroinflammation, and promoted microglial activation, which were reversed through the ablation of microglia. TMAO exacerbates cognitive dysfunction and neuroinflammation in POCD mice, with microglial activation playing a crucial role in this process. Our findings may provide new therapeutic strategies for managing TMAO-related POCD and improving the quality of life for elderly patients.