The effects of MgSO4 as an anti‐inflammatory agent in pregnant women have been investigated in the last few years. Infections can cause an inflammatory reaction involving the placenta membranes and amniotic cavity. They may have short‐term effects on the mother and her fetuses, like preterm birth, cerebral palsy, and developmental delay. Despite the alleged advantages of MgSO4 as a neuroprotective agent in the preterm brain, the long‐term molecular and behavioral function of MgSO4 has not been fully elucidated. Here, we investigated the long‐term effect of antenatal MgSO4, during late gestation, on offspring's behavior focusing on cognitive function, motor activity, and social cognition in adolescence and adulthood, and explored its influence on brain gene expression (e.g., ErbB signaling, pro‐inflammatory, and dopaminergic markers) in adulthood. A significant abnormal exploratory behavior of offspring of MgSO4‐treated dams was found compared to the control group in both adolescence and adulthood. Furthermore, we found that adult females exposed to MgSO4 under inflammation displayed working and recognition memory impairment. A reduction in IL‐6 expression was detected in the prefrontal cortex, and hippocampus specimens derived from LPS–Mg‐treated group. In contrast, an imbalanced expression of dopamine 1 and 2 receptors was detected only in prefrontal cortex specimens. Besides, we found that MgSO4 ameliorated the overexpression of the Nrg1 and Erbb4 receptors induced by LPS in the hippocampus. Thus, MgSO4 treatment for preventing brain injuries can adversely affect offspring cognition behavior later in life, depending on the sex and age of the offspring.