Half of all the elements in the universe heavier than iron were created by rapid neutron capture. The theory for this astrophysical `$r$-process was worked out six decades ago and requires an enormous neutron flux to make the bulk of these elements. Where this happens is still debated. A key piece of missing evidence is the identification of freshly-synthesised $r$-process elements in an astrophysical site. Current models and circumstantial evidence point to neutron star mergers as a probable $r$-process site, with the optical/infrared `kilonova emerging in the days after the merger a likely place to detect the spectral signatures of newly-created neutron-capture elements. The kilonova, AT2017gfo, emerging from the gravitational-wave--discovered neutron star merger, GW170817, was the first kilonova where detailed spectra were recorded. When these spectra were first reported it was argued that they were broadly consonant with an outflow of radioactive heavy elements, however, there was no robust identification of any element. Here we report the identification of the neutron-capture element strontium in a re-analysis of these spectra. The detection of a neutron-capture element associated with the collision of two extreme-density stars establishes the origin of $r$-process elements in neutron star mergers, and demonstrates that neutron stars contain neutron-rich matter.