A search for pseudoscalar or scalar bosons decaying to a top quark pair ($\mathrm{t\bar{t}}$) in final states with one or two charged leptons is presented. The analyzed proton-proton collision data was recorded at $\sqrt{s}$ = 13 TeV by the CMS experiment at the CERN LHC and corresponds to an integrated luminosity of 138 fb$^{-1}$. The invariant mass $m_\mathrm{t\bar{t}}$ of the reconstructed $\mathrm{t\bar{t}}$ system and variables sensitive to its spin and parity are used to discriminate against the standard model $\mathrm{t\bar{t}}$ background. Interference between pseudoscalar or scalar boson production and the standard model $\mathrm{t\bar{t}}$ continuum is included, leading to peak-dip structures in the $m_\mathrm{t\bar{t}}$ distribution. An excess of the data above the background prediction, based on perturbative quantum chromodynamics (QCD) calculations, is observed near the kinematic $\mathrm{t\bar{t}}$ production threshold, while good agreement is found for high $m_\mathrm{t\bar{t}}$. The data are consistent with the background prediction if the contribution from the production of a color-singlet ${}^1\mathrm{S}_0^{[1]}$$\mathrm{t\bar{t}}$ quasi-bound state $η_\mathrm{t}$, predicted by nonrelativistic QCD, is added. Upper limits at 95% confidence level are set on the coupling between the pseudoscalar or scalar bosons and the top quark for boson masses in the range 365$-$1000 GeV, relative widths between 0.5 and 25%, and two background scenarios with or without $η_\mathrm{t}$ contribution.