The development of low-cost efficient bifunctional oxygen electrocatalysts is of importance for optimizing the performance of metal-air batteries. By using manganese dioxide spheres as both the redox initiator and the self-sacrificing template for the in-situ interfacial polymerization of aniline monomers, we demonstrated a facile approach to preparing porous polyaniline spheres in the presence of phytic acid. Subsequent pyrolysis led to nitrogen and phosphorous co-doped carbon spheres (NPCSs) with highly porous structure and good bifunctional electrocatalytic activities for oxygen reduction reaction (ORR) and oxygen evolution reaction (OER). Specifically, NPCSs exhibited a comparable half-wave potential (0.83 V vs. RHE) to that of commercial Pt/C, but a larger current density, for ORR and was superior to RuO2 (overpotential, 320 mV) for OER with a smaller overpotential of 310 mV. The Density Functional Theory (DFT) calculations revealed firstly that the heteroatom-doping at the edges of the porous structure plays a dominate role in achieving the high bifunctional catalytic activities. Furthermore, the bifunctional oxygen electrocatalysis enabled the fabrication of high-performance Zn-air batteries in aqueous and solid-state electrolytes, exhibiting large energy density, high power density, and good cycling stability.