Mg doped NiO nanoparticles MgxNi1−xO (x = 0, 0.1, 0.2, 0.3, 0.4) have been synthesized using sol–gel method. Their structure and morphology of the sample were analyzed using X-ray diffractometer (XRD), transmission electron microscope (TEM) and scanning electron microscope (SEM) with energy dispersive X-ray spectrum (EDX). From the XRD analysis, it is observed that the sample can have FCC structure with a particle size of 22 nm. Functional group analysis was carried out using Fourier transform infrared spectroscopy (FTIR) and Ultraviolet visible spectrometer. The obtained band gap was 4.254 eV. Photoluminescence (PL) emission spectra showed blue emission with a strong band at 345 nm. SEM and TEM images confirmed the spherical morphology and high crystalline nature of the samples. EDX spectrum showed the purity of the samples. The magnetic properties of the samples were analyzed using the vibrating sample magnetometer (VSM). 0.3 %Mg doped samples showed room temperature ferromagnetism with higher saturation magnetization. The synthesized nanoparticles were used further for the fabrication of low cost counter electrode for dye-sensitized solar cells (DSSCs). A thin film of pure and Mg doped NiO nanoparticles deposited on Fluorine tin oxide (FTO) coated glass substrate serves as an efficient plate to increase the number of holes and induced the electrocatalytic activity and yield the efficiency of 2.4%.