Short-duration γ-ray bursts are intense flashes of cosmic γ-rays, lasting less than about two seconds, whose origin is unclear. The favoured hypothesis is that they are produced by a relativistic jet created by the merger of two compact stellar objects (specifically two neutron stars or a neutron star and a black hole). This is supported by indirect evidence such as the properties of their host galaxies, but unambiguous confirmation of the model is still lacking. Mergers of this kind are also expected to create significant quantities of neutron-rich radioactive species, whose decay should result in a faint transient, known as a `kilonova', in the days following the burst. Indeed, it is speculated that this mechanism may be the predominant source of stable r-process elements in the Universe. Recent calculations suggest that much of the kilonova energy should appear in the near-infrared spectral range, because of the high optical opacity created by these heavy r-process elements. Here we report optical and near-infrared observations that provide strong evidence for such an event accompanying the short-duration γ-ray burst GRB 130603B. If this, the simplest interpretation of the data, is correct, then it confirms that compact-object mergers are the progenitors of short-duration γ-ray bursts and the sites of significant production of r-process elements. It also suggests that kilonovae offer an alternative, unbeamed electromagnetic signature of the most promising sources for direct detection of gravitational waves.