Hierarchically porous carbon (PC) was synthesized by a templating method, using magnesium salts (Mg(HCO3)2, MgC2O4 and MgO) as template precursors and citric acid as carbon precursor. During the carbonization process, besides the production of MgO particles, many gases (e.g., CO2/NO2/H2O) were also released and acted as a porogen to generate pores in carbon. The resulting composite (MgO@C) was subsequently treated with HCl solution to remove the MgO templates, yielding hierarchically porous carbon. The surface oxygen functional groups over porous carbon were characterized by TPD and XPS, which showed that the PC-bic, synthesized using Mg(HCO3)2 as the template precursor, had the highest value among the PCs. As expected, the PC-bic exhibited the best performances for electrocatalytic reduction of 4-nitrophenol, with a peak current of −135.5 μA at −0.679 V. The effects of 4-nitrophenol concentration, buffer solution pH and scanning rate on the electrocatalytic activities, as well as the stability of PC-bic for the reaction were investigated.