Four-lepton production in proton-proton collisions, $\mathrm{pp}\to (\mathrm{Z}/ \gamma^*)(\mathrm{Z}/\gamma^*) \to 4\ell$, where $\ell = \mathrm{e}$ or $\mu$, is studied at a center-of-mass energy of 13 TeV with the CMS detector at the LHC. The data sample corresponds to an integrated luminosity of 35.9 fb$^{-1}$. The ZZ production cross section, $\sigma(\mathrm{pp} \to \mathrm{Z}\mathrm{Z}) = 17.2 \pm 0.5\text{ (stat) }\pm 0.7\text{ (syst) }\pm 0.4(\mathrm{theo}) \pm 0.4\text{ (lumi)}$ pb, measured using events with two opposite-sign, same-flavor lepton pairs produced in the mass region $60 < m_{\ell^+\ell^-} < $120 GeV, is consistent with standard model predictions. Differential cross sections are measured and are well described by the theoretical predictions. The Z boson branching fraction to four leptons is measured to be $\mathcal{B}(\mathrm{Z}\to 4\ell) = 4.8 \pm 0.2\text{ (stat) }\pm 0.2\text{ (syst) } \pm 0.1\text{ (theo) }\pm 0.1\text{ (lumi) }\times 10^{-6}$ for events with a four-lepton invariant mass in the range 80 $ < m_{4\ell} < $ 100 GeV and a dilepton mass $m_{\ell\ell} > $4 GeV for all opposite-sign, same-flavor lepton pairs. The results agree with standard model predictions. The invariant mass distribution of the four-lepton system is used to set limits on anomalous ZZZ and ZZ$\gamma$ couplings at 95% confidence level: $-0.0012 < f_4^\mathrm{Z} < 0.0010$, $-0.0010 < f_5^\mathrm{Z} < 0.0013$, $-0.0012 < f_4^{\gamma} < 0.0013$, $-0.0012 < f_5^{\gamma} < 0.0013$.