The gas sensing characteristics of magnesium (Mg)-doped titanium dioxide (TiO2) films were investigated using a spray pyrolysis method. Thin films with varying Mg doping concentrations (0, 2.5, and 5 weight percentages) were deposited and tested for their gas detection ability to organic compounds such as ethanol, butanol, toluene, xylene, and formaldehyde at room temperature (RT). Results disclosed that introducing Mg into TiO2 enhanced the gas sensing characteristics, particularly for formaldehyde. Mg doped TiO2 film enhanced the change in electrical resistance during gas adsorption, leading to an increased response in formaldehyde detection. Additionally, the structural, topographical, and optical characteristics of Mg-TiO2 films were examined. The findings suggest that Mg doping can significantly enhance the gas-sensing features of TiO2 films, making them promising candidates for gas-sensing applications.