Gravitational waves were discovered with the detection of binary black-hole mergers 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 kilonova. 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 estimate. 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 burst. 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.