Reasonable design and development of N-doped carbon materials by post-treatment of porous carbon with melamine is an effective route to preparing low-cost and high-performance electrocatalysts toward oxygen reduction reaction. Nevertheless, melamine derivatives have been observed to rapidly escape during pyrolysis, resulting in the undesirable loss of nitrogen atoms and catalytic activity. Here, a confining-pyrolysis method was proposed to accumulate melamine in the micropores of porous carbon to prepare the object N-doped carbon material with an enhanced nitrogen doping. Because the melamine accumulated in the micropores of porous carbon, the object N-doped carbon material shows a significant enhancement in N-atom content, graphitization degree, mesopore volume, electrochemical active surface area and hydrophobic surface. Density functional theory calculations and electrocatalytic tests demonstrate that the object N-doped carbon material delivers a good electrocatalytic oxygen reduction performance. The N-doped carbon electrode displayed a close half-wave potential for alkaline ORR compared to the commercial Pt/C electrocatalyst and showed an excellent cycling stability and methanol tolerance. This work provides a cost-effective method to prepare carbon-based ORR electrocatalyst for energy devices.