Fast radio bursts1,2 are astronomical radio flashes of unknown physical nature with durations of milliseconds. Their dispersive arrival times suggest an extragalactic origin and imply radio luminosities that are orders of magnitude larger than those of all known short-duration radio transients3. So far all fast radio bursts have been detected with large single-dish telescopes with arcminute localizations, and attempts to identify their counterparts (source or host galaxy) have relied on the contemporaneous variability of field sources4 or the presence of peculiar field stars5 or galaxies4. These attempts have not resulted in an unambiguous association6,7 with a host or multi-wavelength counterpart. Here we report the subarcsecond localization of the fast radio burst FRB 121102, the only known repeating burst source8,9,10,11, using high-time-resolution radio interferometric observations that directly image the bursts. Our precise localization reveals that FRB 121102 originates within 100 milliarcseconds of a faint 180-microJansky persistent radio source with a continuum spectrum that is consistent with non-thermal emission, and a faint (twenty-fifth magnitude) optical counterpart. The flux density of the persistent radio source varies by around ten per cent on day timescales, and very long baseline radio interferometry yields an angular size of less than 1.7 milliarcseconds. Our observations are inconsistent with the fast radio burst having a Galactic origin or its source being located within a prominent star-forming galaxy. Instead, the source appears to be co-located with a low-luminosity active galactic nucleus or a previously unknown type of extragalactic source. Localization and identification of a host or counterpart has been essential to understanding the origins and physics of other kinds of transient events, including gamma-ray bursts12,13 and tidal disruption events14. However, if other fast radio bursts have similarly faint radio and optical counterparts, our findings imply that direct subarcsecond localizations may be the only way to provide reliable associations.