Nonlocal qubit interactions are a hallmark of advanced quantum information technologies1,2,3,4,5. The ability to transfer quantum states and generate entanglement over distances much larger than qubit length scales greatly increases connectivity and is an important step towards maximal parallelism and the implementation of two-qubit gates on arbitrary pairs of qubits6. Qubit-coupling schemes based on cavity quantum electrodynamics2,7,8 also offer the possibility of using high-quality-factor resonators as quantum memories3,9. Extending qubit interactions beyond the nearest neighbour is particularly beneficial for spin-based quantum computing architectures, which are limited by short-range exchange interactions10. Despite the rapidly maturing device technology for silicon spin qubits11,12,13,14,15,16, experimental progress towards achieving long-range spin–spin coupling has so far been restricted to interactions between individual spins and microwave photons17,18,19,20. Here we demonstrate resonant microwave-mediated coupling between two electron spins that are physically separated by more than four millimetres. An enhanced vacuum Rabi splitting is observed when both spins are tuned into resonance with the cavity, indicating a coherent interaction between the two spins and a cavity photon. Our results imply that microwave-frequency photons may be used to generate long-range two-qubit gates between spatially separated spins.