To investigate the effect of early intervention with an electronic specialist-led "proactive" model of care on glycemic and clinical outcomes. The Specialist Treatment of Inpatients: Caring for Diabetes in Surgery (STOIC-D Surgery) randomized controlled trial was performed at the Royal Melbourne Hospital. Eligible participants were adults admitted to a surgical ward during the study with either known diabetes or newly detected hyperglycemia (at least one random blood glucose result ≥11.1 mmol/L). Participants were randomized 1:1 to standard diabetes care or the intervention consisting of an early consult by a specialist inpatient diabetes team using electronic tools for patient identification, communication of recommendations, and therapy intensification. The primary outcome was median patient-day mean glucose (PDMG). The key secondary outcome was incidence of health care-associated infection (HAI). Between 12 February 2021 and 17 December 2021, 1,371 admissions met inclusion criteria, with 680 assigned to early intervention and 691 to standard diabetes care. Baseline characteristics were similar between groups. The early intervention group achieved a lower median PDMG of 8.2 mmol/L (interquartile range [IQR] 6.9-10.0 mmol/L) compared with 8.6 mmol/L (IQR 7.2-10.3 mmol/L) in the control group for an estimated difference of -0.3 mmol/L (95% CI -0.4 to -0.2 mmol/L, P < 0.0001). The incidence of HAI was lower in the intervention group (77 [11%] vs. 110 [16%]), for an absolute risk difference of -4.6% (95% CI -8.2 to -1.0, P = 0.016). In surgical inpatients, early diabetes management intervention with an electronic specialist-led diabetes model of care reduces glucose and HAI.