A bottom-up approach based on hydrothermal carbonization of organic molecules has been used to prepare carbon materials doped with either nitrogen or sulfur or both. To generate mesopores, ZnCl2 has been used as removable structure-directing agent. The final mean mesopore size depended on the type of dopant element. The doped materials exhibited remarkable activity as electrocatalyst in oxygen reduction reaction with nearly complete selectivity to H2O2 synthesis. Two pyrolysis temperatures (973 K and 1173 K) were used that yield materials with different electric conductivity, dopant content and porosity but comparable electrocatalytic performance. N-doped catalyst with an intermediate nitrogen content (4 wt%) and around 80% of pore volume in the mesopore range provided the best performance among the catalysts tested.