Using first-principles calculations we investigate the impact of intrinsic defects and small polarons on the electrical and optical properties of SrTiO3. We pay special attention to the seemingly contradictory role of oxygen vacancies as shallow donor and source of deep-level luminescence, as reported in the literature. We find that oxygen vacancies are double donors, and that one electron is easily ionized, explaining the shallow donor behavior. The second electron is trapped in the form of a small polaron, and this additional binding energy explains the behavior as a deep center that gives rise to blue luminescence. At low temperatures, holes become self-trapped, and recombination of free electrons with self-trapped holes gives rise to green luminescence. These results explain the intricate interplay between the observed green and blue luminescence in SrTiO3, and form a framework for interpreting similar phenomena in other complex oxides.