A ketogenic diet (KD) is a high-fat, low-carbohydrate, and low-protein diet that exerts antiepileptic effects by attenuating spontaneous recurrent seizures, ameliorating learning and memory impairments, and modulating the gut microbiota composition. However, the role of the gut microbiome in the antiepileptic effects of a KD on temporal lobe epilepsy (TLE) induced by lithium-pilocarpine in adult rats is still unknown. Our study provides evidence demonstrating that a KD effectively mitigates seizure behavior and reduces acute-phase epileptic brain activity and that KD treatment alleviates hippocampal neuronal damage and improves cognitive impairment induced by TLE. We also observed that the beneficial effects of a KD are compromised when the gut microbiota is disrupted through antibiotic administration. Analysis of gut microbiota components via 16S rRNA gene sequencing in fecal samples collected from TLE rats fed either a KD or a normal diet. The Chao1 and ACE indices showed decreased species variety in KD-fed rats compared to TLE rats fed a normal diet. A KD increased the levels of Actinobacteriota, Verrucomicrobiota and Proteobacteria and decreased the level of Bacteroidetes. Interestingly, the abundances of Actinobacteriota and Verrucomicrobiota were positively correlated with learning and memory ability, and the abundance of Proteobacteria was positively correlated with seizure susceptibility. In conclusion, our study revealed the significant antiepileptic and neuroprotective effects of a KD on pilocarpine-induced epilepsy in rats, primarily mediated through the modulation of the gut microbiota. However, whether the gut microbiota mediates the antiseizure effects of a KD still needs to be better elucidated.