Prenatal opioid exposure (POE) induces long-term neurodevelopmental, behavioral and cognitive deficits that currently have no treatment options. The mechanisms underlying these symptoms are poorly understood, but have been linked to a range of central, peripheral, and enteric nervous system changes. Emerging evidence indicates that maternal microbiota changes may contribute to these long-term deficits in offspring. Here we test the efficacy of the short-chain fatty acid sodium butyrate (NaB), known to be reduced with chronic opioid use, in mitigating POE-related deficits. Both methadone and sodium butyrate treatments disrupted dam and offspring microbiota composition and function: notably methadone altered dam microbial gene expression of critical enzymes for butyric acid production. Similar disruptions were observed in pups at postnatal day 21 but resolved in adulthood. POE induced anxiety in adolescence on the open-field test, and adult deficits in working spatial memory (TUNL task) and attentional processing (5-CSRTT), which were partially rescued in rats that had received prenatal NaB. NaB treatment mediated the impact of POE on reduced myelination. In summary, these results indicate that early treatment with NaB could significantly improve the outcomes of children born with POE. Furthermore, they highlight the link between maternal microbiota health and offspring cognition in animal models of POE.