The structural and optical properties of magnesium-doped titanium dioxide (Mg–TiO₂) nanocrystalline films were investigated for potential application as photoanodes in dye-sensitized solar cells (DSSCs). The films were synthesized via a sol-gel method using titanium(IV) isopropoxide and magnesium acetate as precursors. Mg doping concentrations ranging from 0 to 4 mol% were explored. The films were deposited onto glass substrates using the doctor blade technique and annealed at various temperatures. Characterization was carried out using X-ray diffraction (XRD), scanning electron microscopy with energy-dispersive X-ray spectroscopy (SEM-EDS), Fourier-transform infrared spectroscopy (FTIR), and UV–Vis spectroscopy. XRD analysis confirmed the formation of TiO₂, MgO, and MgTiO₃ phases, with a notable decrease in crystallite size as Mg content increased. The smallest crystallite size of 12.71 nm was obtained at 4 mol% Mg doping. SEM images revealed improved surface morphology and more uniform porosity in doped films. FTIR spectra indicated no significant changes in chemical bonding, while UV–Vis analysis showed a decrease in bandgap energy from 3.8 eV to 3.4 eV with Mg doping. These modifications suggest enhanced dye adsorption and reduced charge recombination, indicating the potential of Mg-doped TiO₂ films to improve DSSC performance.