Municipal wastewater and sewage sludge is an abundant source of phosphorus (P), but its usage is often limited due to wastewater treatment methods and contaminants, mostly heavy metals (HM's). Three compartment (3C) electrodialysis (ED) was used to simultaneously extract HM's (Cd, Cr, Cu, Ni, Pb and Zn) and recover P from municipal sludge samples obtained at different stages during wastewater treatment involving biological and chemical treatment as well as polymer addition for thickening of sludge and anaerobic digestion of excess sludge. Direct P recovery was investigated for high P reject water stream using the 3C ED cell setup and a two-compartment (2C) where the cathode in direct contact with the wastewater while P was extracted to and concentrated in the anolyte. Simultaneous extraction of HM's and recovery of P from wastewater or raw sludge using 3C ED was be most effective at a low pH using anaerobically digested sludge. The hydrolysis of OM during anaerobic digestion and the anaerobic conditions allowed for easier extraction of HM's such as Cd, Ni and Zn as they had fewer adsorption places, and improved P availability and extractability. Extraction of P from high concentration P streams was most effective using a 3C ED cell setup, with the electrodes separated from the sample by ion-exchange membranes. Extraction with the 2C ED cell setup was less effective due to a rise in pH, caused by half reactions at the cathode and subsequent precipitation of P. For either removal of heavy metals or recovery of phosphorus using ED, the end-products in wastewater treatment, like anaerobically digested sludge and reject-water streams, are therefore best to be treated.