A long-standing paradigm in astrophysics is that collisions- or mergers- of two neutron stars (NSs) form highly relativistic and collimated outflows (jets) powering gamma-ray bursts (GRBs) of short (< 2 s) duration. However, the observational support for this model is only indirect. A hitherto outstanding prediction is that gravitational wave (GW) events from such mergers should be associated with GRBs, and that a majority of these GRBs should be off-axis, that is, they should point away from the Earth. Here we report the discovery of the X-ray counterpart associated with the GW event GW170817. While the electromagnetic counterpart at optical and infrared frequencies is dominated by the radioactive glow from freshly synthesized r-process material in the merger ejecta, known as kilonova, observations at X-ray and, later, radio frequencies exhibit the behavior of a short GRB viewed off-axis. Our detection of X-ray emission at a location coincident with the kilonova transient provides the missing observational link between short GRBs and GWs from NS mergers, and gives independent confirmation of the collimated nature of the GRB emission.