In this study, TiO2- and Mg-doped TiO2 nanoparticles with different dopant contents were prepared by sol-gel method. The prepared photocatalysts were characterized with X-ray diffraction (XRD), scanning electron microscopy (SEM), transmission electron microscopy (TEM), Brunauer–Emmett–Teller (BET) and diffuse reflectance spectroscopy (DRS) techniques. The results of BET analysis indicated a pore diameter of 8 nm and surface area of 48.5 m2 g−1. XRD patterns of pure and doped TiO2 nanoparticles at 450°C revealed that all phases are anatase. The particle size obtained from TEM was less than 20 nm. The band gap energy of Mg-doped TiO2 nanoparticles was lower than that of TiO2. The photocatalytic activity of the pure and doped nanoparticles has been compared in the removal of C.I. Acid Red 27 (AR27). The photocatalytic activity of Mg-doped (0.2 mol%) TiO2 for the degradation of AR27 was higher than that of bare TiO2 nanoparticles. Results of total organic carbon analysis and changes in the AR27 UV–Vis peaks indicated 99% mineralization and extinguishing of all peaks in UV and visible regions is possible with Mg-doped TiO2 nanoparticles. Removal efficiency of AR27 was sensitive to the parameters such as catalyst dose, pollutant concentration and light intensity.