Gestational exposure to environmental toxins and socioeconomic stressors are epidemiologically linked to neurodevelopmental disorders with strong male-bias, such as autism. We modeled these prenatal risk factors in mice, by co-exposing pregnant dams to an environmental pollutant and limited-resource stress, which robustly dysregulated the maternal immune system. Only male offspring displayed long-lasting behavioral abnormalities and alterations in the activity of brain networks encoding social interactions. Cellularly, prenatal stressors impaired microglial synaptic pruning within the anterior cingulate cortex, a central node of the social coding network, in males during early postnatal development. Precise inhibition of microglial phagocytosis during the same critical period mimicked the impact of prenatal stressors on a male-specific behavior, indicating that environmental stressors alter neural circuit formation in males via impairing microglia function during development.