  PSR J1023+0038 is the first millisecond pulsar discovered to pulsate in the
visible band; such a detection took place when the pulsar was surrounded by an
accretion disk and also showed X-ray pulsations. We report on the first high
time resolution observational campaign of this transitional pulsar in the disk
state, using simultaneous observations in the optical (TNG, NOT, TJO), X-ray
(XMM-Newton, NuSTAR, NICER), infrared (GTC) and UV (Swift) bands. Optical and
X-ray pulsations were detected simultaneously in the X-ray high intensity mode
in which the source spends $\sim$ 70% of the time, and both disappeared in the
low mode, indicating a common underlying physical mechanism. In addition,
optical and X-ray pulses were emitted within a few km, had similar pulse shape
and distribution of the pulsed flux density compatible with a power-law
relation $F_{\nu} \propto \nu^{-0.7}$ connecting the optical and the 0.3-45 keV
X-ray band. Optical pulses were detected also during flares with a pulsed flux
reduced by one third with respect to the high mode; the lack of a simultaneous
detection of X-ray pulses is compatible with the lower photon statistics. We
show that magnetically channeled accretion of plasma onto the surface of the
neutron star cannot account for the optical pulsed luminosity ($\sim 10^{31}$
erg/s). On the other hand, magnetospheric rotation-powered pulsar emission
would require an extremely efficient conversion of spin-down power into pulsed
optical and X-ray emission. We then propose that optical and X-ray pulses are
instead produced by synchrotron emission from the intrabinary shock that forms
where a striped pulsar wind meets the accretion disk, within a few light
cylinder radii away, $\sim$ 100 km, from the pulsar.
