Results are reported for the B$^0_\mathrm{s}\to\mu^+\mu^-$ branching fraction and effective lifetime and from a search for the decay B$^0\to\mu^+\mu^-$. The analysis uses a data sample of proton-proton collisions accumulated by the CMS experiment in 2011, 2012, and 2016, with center-of-mass energies (integrated luminosities) of 7 TeV (5 fb$^{-1}$), 8 TeV (20 fb$^{-1}$), and 13 TeV (36 fb$^{-1}$). The branching fractions are determined by measuring event yields relative to B$^+\to$J/$\psi$K$^+$ decays (with J/$\psi\to\mu^+\mu^-$), which results in the reduction of many of the systematic uncertainties. The decay B$^0_\mathrm{s}\to\mu^+\mu^-$ is observed with a significance of 5.6 standard deviations. The branching fraction is measured to be $\mathcal{B}$(B$^0_\mathrm{s}\to\mu^+\mu^-$) = [2.9$ ^{+0.7}_{-0.6}$ (exp)$\pm$0.2(frag)]$\times 10^{-9}$, where the first uncertainty combines the experimental statistical and systematic contributions, and the second is due to the uncertainty in the ratio of the B$^0_\mathrm{s}$ and the B$^+$ fragmentation functions. No significant excess is observed for the decay B$^0\to\mu^+\mu^-$, and an upper limit of $\mathcal{B}$(B$^0\to\mu^+\mu^-$) < 3.6$\times$10$^{-10}$ is obtained at 95% confidence level. The B$^0_\mathrm{s}\to\mu^+\mu^-$ effective lifetime is measured to be $\tau_{\mu^+\mu^-}$= 1.70$^{+0.61}_{-0.44}$ ps. These results are consistent with standard model predictions.