  PSR J1023+0038 is the first millisecond pulsar that was ever observed as an
optical and UV pulsar. So far, it is the only optical transitional millisecond
pulsar. The rotation- and accretion-powered emission mechanisms hardly
individually explain the observed characteristics of optical pulsations. A
synergistic model, combining these standard emission processes, was proposed to
explain the origin of the X-ray/UV/optical pulsations. We study the phase lag
between the pulses in the optical and X-ray bands to gain insight into the
physical mechanisms that cause it. We performed a detailed timing analysis of
simultaneous or quasi-simultaneous observations in the X-ray band, acquired
with the XMM-Newton and NICER satellites, and in the optical band, with the
fast photometers SiFAP2 (mounted at the 3.6 m Telescopio Nazionale Galileo) and
Aqueye+ (mounted at the 1.8 m Copernicus Telescope). We estimated the time lag
of the optical pulsation with respect to that in the X-rays by modeling the
folded pulse profiles with two harmonic components. Optical pulses lag the
X-ray pulses by $\sim$ 150 $\mu$s in observations acquired with instruments
(NICER and Aqueye+) whose absolute timing uncertainty is much smaller than the
measured lag. We also show that the phase lag between optical and X-ray
pulsations lies in a limited range of values, $\delta \phi \in$ (0 $-$ 0.15),
which is maintained over timescales of about five years. This indicates that
both pulsations originate from the same region, and it supports the hypothesis
of a common emission mechanism. Our results are interpreted in the shock-driven
mini pulsar nebula scenario. This scenario suggests that optical and X-ray
pulses are produced by synchrotron emission from the shock that formed within a
few light cylinder radii away ($\sim$ 100 km) from the pulsar, where its
striped wind encounters the accretion disk inflow.
