A search for charge-parity (CP) violation in D0→K-K+ and D0→π-π+ decays is reported, using pp collision data corresponding to an integrated luminosity of 5.9 fb-1 collected at a center-of-mass energy of 13 TeV with the LHCb detector. The flavor of the charm meson is inferred from the charge of the pion in D∗(2010)+→D0π+ decays or from the charge of the muon in B̄→D0μ-ν̄μX decays. The difference between the CP asymmetries in D0→K-K+ and D0→π-π+ decays is measured to be ΔACP=[-18.2±3.2(stat)±0.9(syst)]×10-4 for π-tagged and ΔACP=[-9±8(stat)±5(syst)]×10-4 for μ-tagged D0 mesons. Combining these with previous LHCb results leads to ΔACP=(-15.4±2.9)×10-4, where the uncertainty includes both statistical and systematic contributions. The measured value differs from zero by more than 5 standard deviations. This is the first observation of CP violation in the decay of charm hadrons.