The first study of charm quark diffusion with respect to the jet axis in heavy ion collisions is presented. The measurement is performed using jets with pTjet>60 GeV/c and D0 mesons with pTD>4 GeV/c in lead-lead (Pb-Pb) and proton-proton (pp) collisions at a nucleon-nucleon center-of-mass energy of sNN=5.02 TeV, recorded by the CMS detector at the LHC. The radial distribution of D0 mesons with respect to the jet axis is sensitive to the production mechanisms of the meson, as well as to the energy loss and diffusion processes undergone by its parent parton inside the strongly interacting medium produced in Pb-Pb collisions. When compared to Monte Carlo event generators, the radial distribution in pp collisions is found to be well described by pythia, while the slope of the distribution predicted by sherpa is steeper than that of the data. In Pb-Pb collisions, compared to the pp results, the D0 meson distribution for 4<pTD<20 GeV/c hints at a larger distance on average with respect to the jet axis, reflecting a diffusion of charm quarks in the medium created in heavy ion collisions. At higher pTD, the Pb-Pb and pp radial distributions are found to be similar.