We investigated the formation mechanisms of the nucleobases adenine and guanine and the nucleobase analogues hypoxanthine, xanthine, isoguanine, and 2,6-diaminopurine in a UV-irradiated mixed 10:1 H2O:NH3 ice seeded with precursor purine by using ab initio and density functional theory computations. Our quantum chemical investigations suggest that a multistep reaction mechanism involving purine cation, hydroxyl and amino radicals, together with water and ammonia, explains the experimentally obtained products in an independent study. The relative abundances of these products appear to largely follow from relative thermodynamic stabilities. The key role of the purine cation is likely to be the reason why purine is not functionalized in pure ammonia ice, where cations are promptly neutralized by free electrons from NH3 ionization. Amine group addition to purine is slightly favored over hydroxyl group attachment based on energetics, but hydroxyl is much more abundant due to higher abundance of H2O. The amino group is preferentially attached to the 6 position, giving 6-aminopurine, that is, adenine, while the hydroxyl group is preferentially attached to the 2 position, leading to 2-hydroxypurine. A second substitution by hydroxyl or amino group occurs at either the 6 or the 2 position depending on the first substitution. Given that H2O is far more abundant than NH3 in the experimentally studied ices (as well as based on interstellar abundances), xanthine and isoguanine are expected to be the most abundant bi-substituted photoproducts. Key Words: Astrophysical ice-Abiotic organic synthesis-Nucleic acids-Origin of life-RNA world. Astrobiology 17, 771-785.