Solar flares - the most powerful explosions in the solar system - are also efficient particle accelerators, capable of energizing a large number of charged particles to relativistic speeds. A termination shock is often invoked in the standard model of solar flares as a possible driver for particle acceleration, yet its existence and role have remained controversial. We present observations of a solar flare termination shock and trace its morphology and dynamics using high-cadence radio imaging spectroscopy. We show that a disruption of the shock coincides with an abrupt reduction of the energetic electron population. The observed properties of the shock are well-reproduced by simulations. These results strongly suggest that a termination shock is responsible, at least in part, for accelerating energetic electrons in solar flares.