The decays of the 125~\mathrm{GeV} Higgs boson into a Z boson and a or meson are searched for using proton-proton collision data collected by the CMS experiment at the LHC from 2016 to 2018 at \sqrt{s}=13~\mathrm{TeV}. The analysed data set corresponds to an integrated luminosity of 137~\mathrm{fb}^{-1}. Events are selected in which the Z boson decays into a pair of electrons or a pair of muons, and the and mesons decay into pairs of pions and kaons, respectively. No significant excess above the background model is observed. As different polarization states are possible for the decay products of the Z boson and or mesons, affecting the signal acceptance, scenarios in which the decays are longitudinally or transversely polarized are considered. Upper limits at the confidence level on the Higgs boson branching fractions into Z and Z are determined to be and , respectively, where the ranges reflect the considered polarization scenarios; these values are and times larger than the respective standard model expectations. These results constitute the first experimental limits on these two decay channels.