The recent discovery of a gamma-ray burst (GRB) coincident with the gravitational-wave (GW) event GW170817 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 is an analogue of GRB170817A located at a cosmological distance. GRB150101B is a faint short burst 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, while the observed X-rays trace the GRB afterglow viewed at an angle of 13°. Our findings suggest that these properties could be common among future electromagnetic counterparts of GW sources.