  A neutrino with energy of $\sim$290 TeV, IceCube-170922A, was detected in
coincidence with the BL Lac object TXS~0506+056 during enhanced gamma-ray
activity, with chance coincidence being rejected at $\sim 3\sigma$ level. We
monitored the object in the very-high-energy (VHE) band with the MAGIC
telescopes for $\sim$41 hours from 1.3 to 40.4 days after the neutrino
detection. Day-timescale variability is clearly resolved. We interpret the
quasi-simultaneous neutrino and broadband electromagnetic observations with a
novel one-zone lepto-hadronic model, based on interactions of electrons and
protons co-accelerated in the jet with external photons originating from a
slow-moving plasma sheath surrounding the faster jet spine. We can reproduce
the multiwavelength spectra of TXS 0506+056 with neutrino rate and energy
compatible with IceCube-170922A, and with plausible values for the jet power of
$\sim 10^{45} - 4 \times 10^{46} {\rm erg \ s^{-1}}$. The steep spectrum
observed by MAGIC is concordant with internal $\gamma\gamma$ absorption above a
few tens of GeV entailed by photohadronic production of a $\sim$290 TeV
neutrino, corroborating a genuine connection between the multi-messenger
signals. In contrast to previous predictions of predominantly hadronic emission
from neutrino sources, the gamma-rays can be mostly ascribed to inverse Compton
up-scattering of external photons by accelerated electrons. The X-ray and VHE
bands provide crucial constraints on the emission from both accelerated
electrons and protons. We infer that the maximum energy of protons in the jet
co-moving frame can be in the range $\sim 10^{14}$ to $10^{18}$ eV.
