Aims. Photodissociation by UV light is an important destruction mechanism for CO in many astrophysical environments, ranging from interstellar clouds to protoplanetary disks. The aim of this work is to gain a better understanding of the depth dependence and isotope-selective nature of this process. Methods. We present a photodissociation model based on recent spectroscopic data from the literature, which allows us to compute depth-dependent and isotope-selective photodissociation rates at higher accuracy than in previous work. The model includes self-shielding, mutual shielding and shielding by atomic and molecular hydrogen, and it is the first such model to include the rare isotopologues C17O and 13C17O. We couple it to a simple chemical network to analyse CO abundances in diffuse and translucent clouds, photon-dominated regions, and circumstellar disks. Results. The photodissociation rate in the unattenuated interstellar radiation field is 2.6e-10 s^-1, 30% higher than currently adopted values. Increasing the excitation temperature or the Doppler width can reduce the photodissociation rates and the isotopic selectivity by as much as a factor of three for temperatures above 100 K. The model reproduces column densities observed towards diffuse clouds and PDRs, and it offers an explanation for both the enhanced and the reduced N(12CO)/N(13CO) ratios seen in diffuse clouds. The photodissociation of C17O and 13C17O shows almost exactly the same depth dependence as that of C18O and 13C18O, respectively, so 17O and 18O are equally fractionated with respect to 16O. This supports the recent hypothesis that CO photodissociation in the solar nebula is responsible for the anomalous 17O and 18O abundances in meteorites.