A measurement of the lifetimes of the Ωc0 and Ξc0 baryons is reported using proton-proton collision data at a centre-of-mass energy of 13 TeV, corresponding to an integrated luminosity of 5.4 fb−1 collected by the LHCb experiment. The Ωc0 and Ξc0 baryons are produced directly from proton interactions and reconstructed in the pK−K−π+ final state. The Ωc0 lifetime is measured to be 276.5±13.4±4.4±0.7 fs, and the Ξc0 lifetime is measured to be 148.0±2.3±2.2±0.2 fs, where the first uncertainty is statistical, the second systematic, and the third due to the uncertainty on the D0 lifetime. These results confirm previous LHCb measurements based on semileptonic beauty-hadron decays, which disagree with earlier results of a four times shorter Ωc0 lifetime, and provide the single most precise measurement of the Ωc0 lifetime.