Electrochemical CO2 reduction (CO2R) is a promising avenue for the conversion of CO2 into fuels and beneficial chemicals. Significant efforts have been devoted to the development of active and selective electrocatalysts for CO2R. Atomically dispersed transition metal electrocatalysts have recently attracted consideration for CO2R due to their unique electronic and structural properties that can impart good catalytic performance and high active metal utilization. Among different precursors used for preparing these catalysts, metal–organic frameworks (MOFs) have been utilized as templates for generating atomically dispersed transition metal active sites as they provide well-defined structures that contain transition metals isolated from each other by organic ligands, along with high surface areas. However, maintaining the isolation of the transition metal species, achieving high surface concentrations of active sites, and imparting porosity during the high-temperature heat treatment of MOFs used during synthesis remain a challenge. In this report, Mg2+ ions have been employed as spacers in a bimetallic metal–organic framework (NiMg-MOF-74) to assist in preventing the coalescence of the Ni atoms into Ni-based particles during heat treatment, which combined with the use of urea as a nitrogen source resulted in the formation of isolated Ni–Nx/C active sites. Our findings demonstrated that Mg2+ ions play a crucial role in extending the distance between the adjacent Ni sites in the precursor structure and generating atomically isolated Ni sites. On the contrary, the utilization of Mg-free Ni-MOF-74 led to the formation of metallic Ni particles embedded in a carbon nanotube-based structure. Furthermore, we investigated the impact of pyrolysis temperature on the produced catalyst morphology. The generation of isolated Ni–Nx/C sites was promoted at higher pyrolysis temperatures (900 °C), while Ni-based particles were predominantly formed at a lower temperature (700 °C). The optimized atomically dispersed Ni–N–C catalyst exhibited excellent selectivity toward CO with a Faradaic efficiency of ∼90% and a current density of −4.2 mA/cm2 at −0.76 V vs a reversible hydrogen electrode.