We present a measurement of the Z boson differential cross section in rapidity and transverse momentum using a data sample of pp collision events at a centre-of-mass energy sqrt(s)=8 TeV, corresponding to an integrated luminosity of 19.7 inverse femtobarns. The Z boson is identified via its decay to a pair of muons. The measurement provides a precision test of quantum chromodynamics over a large region of phase space. In addition, due to the small experimental uncertainties in the measurement the data has the potential to constrain the gluon parton distribution function in the kinematic regime important for Higgs boson production via gluon fusion. The results agree with the next-to-next-to-leading-order predictions computed with the FEWZ program. The results are also compared to the commonly used leading-order MADGRAPH and next-to-leading-order POWHEG generators.