The recent discovery of a faint gamma-ray burst (GRB) coincident with the gravitational wave (GW) event GW 170817 revealed the existence of a population of low-luminosity short duration gamma-ray transients produced by neutron star mergers in the nearby Universe. These events could be routinely detected by existing gamma-ray monitors, yet previous observations failed to identify them without the aid of GW triggers. Here we show that GRB150101B was an analogue of GRB170817A located at a cosmological distance. GRB 150101B was a faint short duration GRB characterized by a bright optical counterpart and a long-lived X-ray afterglow. These properties are unusual for standard short GRBs and are instead consistent with an explosion viewed off-axis: the optical light is produced by a luminous kilonova component, while the observed X-rays trace the GRB afterglow viewed at an angle of ~13 degrees. Our findings suggest that these properties could be common among future electromagnetic counterparts of GW sources.