The production of jets in association with Z bosons, reconstructed via the μ+μ- and e+e- decay channels, is studied in pp and, for the first time, in Pb-Pb collisions. Both data samples were collected by the CMS experiment at the LHC, at a nucleon-nucleon center-of-mass energy of 5.02 TeV. The Pb-Pb collisions were analyzed in the 0%-30% centrality range. The back-to-back azimuthal alignment was studied in both pp and Pb-Pb collisions for Z bosons with transverse momentum pTZ>60 GeV/c and a recoiling jet with pTjet>30 GeV/c. The pT imbalance xjZ=pTjet/pTZ, as well as the average number of jet partners per Z, RjZ, was studied in intervals of pTZ. The RjZ is found to be smaller in Pb-Pb than in pp collisions, which suggests that in Pb-Pb collisions a larger fraction of partons associated with the Z bosons fall below the 30 GeV/c pTjet threshold because they lose energy.