A supported cobalt catalyst with atomically dispersed Co-Nx sites (3.5 wt% Co) in a mesoporous N-doped carbon matrix (named Co@mesoNC) is synthesized by hydrolysis of tetramethyl orthosilicate (TMOS) in a Zn/Co bimetallic zeolitic imidazolate framework (BIMZIF(Co,Zn)), followed by high-temperature pyrolysis and SiO2 leaching. A combination of TEM, XRD XPS and X-ray absorption spectroscopy studies confirm the absence of cobalt nanoparticles and indicate that these highly dispersed cobalt species are present in the form of Co-Nx. The exclusive formation of Co-Nx sites in the carbon matrix is attributed to the presence of a large amount of Zn and N in the BIMZIF precursor together with the presence of SiO2 in the pore space of this framework, extending the initial spatial distance between cobalt atoms and thereby impeding their agglomeration. The presence of SiO2 during high-temperature pyrolysis is proven crucial to create mesoporosity and a high BET area and pore volume in the N-doped carbon support (1780 m2 g−1, 1.54 cm3 g−1). This heterogeneous Co@mesoNC catalyst displays high activity and selectivity (>99%) for the selective hydrogenation of nitrobenzene to aniline at mild conditions (0.5–3 MPa, 343–383 K). When more challenging substrates (functionalized nitroarenes) are hydrogenated, the catalyst Co@mesoNC displays an excellent chemoselectivity to the corresponding substituted anilines. The presence of mesoporosity improves mass transport of reactants and/or products and the accessibility of the active Co-Nx sites, and greatly reduces deactivation due to fouling.