Error correction is important in classical and quantum computation. Decoherence caused by the inevitable interaction of quantum bits with their environment leads to dephasing or even relaxation. Correction of the concomitant errors is therefore a fundamental requirement for scalable quantum computation. Although algorithms for error correction have been known for some time, experimental realizations are scarce. Here we show quantum error correction in a heterogeneous, solid-state spin system. We demonstrate that joint initialization, projective readout and fast local and non-local gate operations can all be achieved in diamond spin systems, even under ambient conditions. High-fidelity initialization of a whole spin register (99 per cent) and single-shot readout of multiple individual nuclear spins are achieved by using the ancillary electron spin of a nitrogen-vacancy defect. Implementation of a novel non-local gate generic to our electron-nuclear quantum register allows the preparation of entangled states of three nuclear spins, with fidelities exceeding 85 per cent. With these techniques, we demonstrate three-qubit phase-flip error correction. Using optimal control, all of the above operations achieve fidelities approaching those needed for fault-tolerant quantum operation, thus paving the way to large-scale quantum computation. Besides their use with diamond spin systems, our techniques can be used to improve scaling of quantum networks relying on phosphorus in silicon, quantum dots, silicon carbide or rare-earth ions in solids.