Abstract Phosphate pollution significantly contributes to eutrophication and the degradation of aquatic ecosystems. The removal of phosphate from wastewater before discharging into the environment is essential for the sustainability of the ecosystem. This work focuses on using a polarity reversal mode electrocoagulation (PRM-EC) system integrated with iron (Fe) and aluminum (Al) electrodes to remove phosphate from wastewater. The conditions for the removal process were optimized using response surface methodology (RSM). Central composite design (CCD) was used to design the experiment and numerical optimization was utilized to find the optimal conditions. The phosphate removal efficiency could reach 93.12% at a current density of 40 A m−2, time of 30 min, pH of 6.4, and electrode distance of 10.5 mm. The energy consumption was about 0.4 kW m−3. The artificial neural network (ANN) modeling showed that the current density was the most influencing factor, followed by time, pH, and electrode distance. The mechanism underlying the PRM-EC process encompassed electrode dissolution, floc formation, phosphate adsorption, and precipitation. The findings in the work show that PRM-EC is an environmentally friendly and effective solution for phosphate removal.