Antibacterial restorative materials against caries-causing bacteria are highly preferred among high-risk patients, such as the elderly, and patients with metabolic diseases such as diabetes. This study aimed to enhance the antibacterial potential of resin composite with Magnesium-doped Zinc oxide (Mg-doped ZnO) nanoparticles (NPs) and to look for their effectiveness in the alloxan-induced diabetic model. Hexagonal Mg-doped ZnO NPs (22.3 nm diameter) were synthesized by co-precipitation method and characterized through ultraviolet-visible (UV-Vis), Fourier transform infrared (FTIR) spectroscopy, X-ray diffraction (XRD), scanning electron microscopy (SEM), and energy dispersive spectroscopy (EDS) analysis. The Mg-doped ZnO NPs (1, 2.5 and 5% w/w) were then evaluated for antibacterial activity using a closed system in vitro biofilm model. Significant enhancement in the antibacterial properties was observed in composites with 1% Mg-doped ZnO compared to composites with bare ZnO reinforced NPs (Streptococcus mutans, p = 0.0005; Enterococcus faecalis, p = 0.0074, Saliva microcosm, p < 0.0001; Diabetic Saliva microcosm, p < 0.0001). At 1–2.5% Mg-doped ZnO NPs concentration, compressive strength and biocompatibility of composites were not affected. The pH buffering effect was also achieved at these concentrations, hence not allowing optimal conditions for the anaerobic bacteria to grow. Furthermore, composites with Mg-doped ZnO prevented secondary caries formation in the secondary caries model of alloxan-induced diabetes. Therefore, Mg-doped ZnO NPs are highly recommended as an antibacterial agent for resin composites to avoid biofilm and subsequent secondary caries formation in high-risk patients.