It is thought that neutron stars in low-mass binary systems can accrete matter and angular momentum from the companion star and be spun-up to millisecond rotational periods. During the accretion stage, the system is called a low-mass X-ray binary, and bright X-ray emission is observed. When the rate of mass transfer decreases in the later evolutionary stages, these binaries host a radio millisecond pulsar whose emission is powered by the neutron star's rotating magnetic field. This evolutionary model is supported by the detection of millisecond X-ray pulsations from several accreting neutron stars and also by the evidence for a past accretion disc in a rotation-powered millisecond pulsar. It has been proposed that a rotation-powered pulsar may temporarily switch on during periods of low mass inflow in some such systems. Only indirect evidence for this transition has hitherto been observed. Here we report observations of accretion-powered, millisecond X-ray pulsations from a neutron star previously seen as a rotation-powered radio pulsar. Within a few days after a month-long X-ray outburst, radio pulses were again detected. This not only shows the evolutionary link between accretion and rotation-powered millisecond pulsars, but also that some systems can swing between the two states on very short timescales.