Observations and modelling of an optical transient counterpart to a gravitational-wave event and γ-ray burst reveal that neutron-star mergers produce gravitational waves and radioactively powered kilonovae, and are a source of heavy elements. Merging neutron stars are potential sources of gravitational waves and have long been predicted to produce jets of material as part of a low-luminosity transient known as a 'kilonova'. There is growing evidence that neutron-star mergers also give rise to short, hard gamma-ray bursts. A group of papers in this issue report observations of a transient associated with the gravitational-wave event GW170817—a signature of two neutron stars merging and a gamma-ray flash—that was detected in August 2017. The observed gamma-ray, X-ray, optical and infrared radiation signatures support the predictions of an outflow of matter from double neutron-star mergers and present a clear origin for gamma-ray bursts. Previous predictions differ over whether the jet material would combine to form light or heavy elements. These papers now show that the early part of the outflow was associated with lighter elements whereas the later observations can be explained by heavier elements, the origins of which have been uncertain. However, one paper (by Stephen Smartt and colleagues) argues that only light elements are needed for the entire event. Additionally, Eleonora Troja and colleagues report X-ray observations and radio emissions that suggest that the 'kilonova' jet was observed off-axis, which could explain why gamma-ray-burst detections are seen as dim. Gravitational waves were discovered with the detection of binary black-hole mergers1 and they should also be detectable from lower-mass neutron-star mergers. These are predicted to eject material rich in heavy radioactive isotopes that can power an electromagnetic signal. This signal is luminous at optical and infrared wavelengths and is called a kilonova2,3,4,5. The gravitational-wave source GW170817 arose from a binary neutron-star merger in the nearby Universe with a relatively well confined sky position and distance estimate6. Here we report observations and physical modelling of a rapidly fading electromagnetic transient in the galaxy NGC 4993, which is spatially coincident with GW170817 and with a weak, short γ-ray burst7,8. The transient has physical parameters that broadly match the theoretical predictions of blue kilonovae from neutron-star mergers. The emitted electromagnetic radiation can be explained with an ejected mass of 0.04 ± 0.01 solar masses, with an opacity of less than 0.5 square centimetres per gram, at a velocity of 0.2 ± 0.1 times light speed. The power source is constrained to have a power-law slope of −1.2 ± 0.3, consistent with radioactive powering from r-process nuclides. (The r-process is a series of neutron capture reactions that synthesise many of the elements heavier than iron.) We identify line features in the spectra that are consistent with light r-process elements (atomic masses of 90–140). As it fades, the transient rapidly becomes red, and a higher-opacity, lanthanide-rich ejecta component may contribute to the emission. This indicates that neutron-star mergers produce gravitational waves and radioactively powered kilonovae, and are a nucleosynthetic source of the r-process elements.