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 (tt¯H) 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 tt¯H process is observed with a significance of 5.2 standard deviations. The measured cross section times H→γγ branching ratio is 1.64-0.36+0.38(stat)-0.14+0.17(sys) fb, and the measured rate for tt¯H is 1.43-0.31+0.33(stat)-0.15+0.21(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.