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