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 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 NGC4993, which is spatially coincident with GW170817 and a weak short gamma-ray burst. The transient has physical parameters broadly matching 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 Msol, with an opacity of kappa <= 0.5 cm2/gm at a velocity of 0.2 +/- 0.1c. The power source is constrained to have a power law slope of beta = -1.2 +/- 0.3, consistent with radioactive powering from r-process nuclides. We identify line features in the spectra that are consistent with light r-process elements (90 < A < 140). As it fades, the transient rapidly becomes red, and emission may have contribution by a higher opacity, lanthanide-rich ejecta component. This indicates that neutron star mergers produce gravitational waves, radioactively powered kilonovae, and are a nucleosynthetic source of the r-process elements.