The taxonomy of optical emission detected during the critical first few minutes after the onset of a gamma-ray burst (GRB) defines two broad classes: prompt optical emission correlated with prompt gamma-ray emission, and early optical afterglow emission uncorrelated with the gamma-ray emission. The standard theoretical interpretation attributes prompt emission to internal shocks in the ultra-relativistic outflow generated by the internal engine; early afterglow emission is attributed to shocks generated by interaction with the surrounding medium. Here we report on observations of a bright GRB that, for the first time, clearly show the temporal relationship and relative strength of the two optical components. The observations indicate that early afterglow emission can be understood as reverberation of the energy input measured by prompt emission. Measurements of the early afterglow reverberations therefore probe the structure of the environment around the burst, whereas the subsequent response to late-time impulsive energy releases reveals how earlier flaring episodes have altered the jet and environment parameters. Many GRBs are generated by the death of massive stars that were born and died before the Universe was ten per cent of its current age, so GRB afterglow reverberations provide clues about the environments around some of the first stars.