In the present work, the optoelectronic properties of InP and InN alloys doped with Mg through substitution of indium atoms (with an Mg concentration of \(x = 0.25\) ) have been investigated using density functional theory within the generalized gradient approximation. To assess the experimental feasibility of these alloys, their structural, thermodynamic, and dynamical stability were examined through cohesive energy, formation enthalpies, and phonon calculations. The calculated electronic density of states revealed band gaps of 1.27 and 0.55 eV for InP and InN, respectively. Moreover, in the Mg-doped alloys, the Fermi level crosses the electronic states, suggesting a metallic-like behavior. In addition, a gap-like separation is observed within the conduction band for both systems, indicating a redistribution of unoccupied electronic states after alloying. The maximum absorption coefficients for InP, InN, In \(_{0.75}\) Mg \(_{0.25}\) P, and In \(_{0.75}\) Mg \(_{0.25}\) N were found to be 1.42 \(\times\) 10 \(^8\) m \(^{-1}\) (at 6.31 eV), 1.95 \(\times\) 10 \(^8\) m \(^{-1}\) (at 5.96 eV), 1.48 \(\times\) 10 \(^8\) m \(^{-1}\) (at 7.36 eV), and 1.62 \(\times\) 10 \(^8\) m \(^{-1}\) (at 6.19 eV), respectively. These results suggest that the studied compounds are possible absorbers in the ultraviolet region. Furthermore, InP and InN are possible candidates for solar cell and photocatalytic applications, respectively, due to their favorable band gaps and strong absorption coefficients, while their Mg-doped counterparts may be useful for plasmonic applications.