  Gamma-ray bursts (GRBs) are the most electromagnetically luminous cosmic
explosions. They are powered by collimated streams of plasma (jets) ejected by
a newborn stellar-mass black hole or neutron star at relativistic velocities
(near the speed of light). Their short-lived (typically tens of seconds) prompt
$\gamma$-ray emission from within the ejecta is followed by long-lived
multi-wavelength afterglow emission from the ultra-relativistic forward shock.
This shock is driven into the circumburst medium by the GRB ejecta that are in
turn decelerated by a mildly-relativistic reverse shock. Forward shock emission
was recently detected up to teraelectronvolt-energy $\gamma$-rays, and such
very-high-energy emission was also predicted from the reverse shock. Here we
report the detection of optical and gigaelectronvolt-energy $\gamma$-ray
emission from GRB 180720B during the first few hundred seconds, which is
explained by synchrotron and inverse-Compton emission from the reverse shock
propagating into the ejecta, implying a low-magnetization ejecta. Our optical
measurements show a clear transition from the reverse shock to the forward
shock driven into the circumburst medium, accompanied by a 90-degree change in
the mean polarization angle and fluctuations in the polarization degree and
angle. This indicates turbulence with large-scale toroidal and
radially-stretched magnetic field structures in the reverse and forward shocks,
respectively, which tightly couple to the physics of relativistic shocks and
GRB jets -- launching, composition, dissipation and particle acceleration.
