A study of the charge conjugation and parity (CP) properties of the interaction between the Higgs boson and top quarks is presented. Higgs bosons are identified via the diphoton decay channel (H → γγ), and their production in association with a top quark pair (t¯tH) or single top quark (tH) is studied. The analysis uses 139 fb−1 of proton–proton collision data recorded at a center-of-mass energy of √s= 13 TeV with the ATLAS detector at the Large Hadron Collider. Assuming a CP-even coupling, the t¯tH process is observed with a significance of 5.2 standard deviations. The measured cross section times H → γγ branching ratio is 1.64+0.38−0.36(stat)+0.17−0.14(sys)fb, and the measured rate for t¯tH is 1.43+0.33−0.31(stat)+0.21−0.15(sys) times the Standard Model expectation. The tH production process is not observed and an upper limit on its rate of 12 times the Standard Model expectation is set. A CP-mixing angle greater (less) than 43 (−43)° is excluded at 95% confidence level.