Even if the fundamental action of gravity is local, the corresponding quantum effective action, that includes the effect of quantum fluctuations, is a nonlocal object. These nonlocalities are well understood in the ultraviolet regime but much less in the infrared, where they could in principle give rise to important cosmological effects. Here we systematize and extend previous work of our group, in which it is assumed that a mass scale \Lambda is dynamically generated in the infrared, giving rise to nonlocal terms in the quantum effective action of gravity. We give a detailed discussion of conceptual aspects related to nonlocal gravity and of the cosmological consequences of these models. The requirement of providing a viable cosmological evolution severely restricts the form of the nonlocal terms, and selects a model (the so-called RR model) that corresponds to a dynamical mass generation for the conformal mode. For such a model: (1) there is a FRW background evolution, where the nonlocal term acts as an effective dark energy with a phantom equation of state, providing accelerated expansion without a cosmological constant. (2) Cosmological perturbations are well behaved. (3) Implementing the model in a Boltzmann code and comparing with observations we find that the RR model fits the CMB, BAO, SNe, structure formation data and local H0 measurements at a level statistically equivalent to \LambdaCDM. (4) Bayesian parameter estimation shows that the value of H0 obtained in the RR model is higher than in \LambdaCDM, reducing to 2.0\sigma the tension with the value from local measurements. (5) The RR model provides a prediction for the sum of neutrino masses that falls within the limits set by oscillation and terrestrial experiments. (6) Gravitational waves propagate at the speed of light, complying with the limit from GW170817/GRB 170817A.