Cementing is an essential downhole operation during the drilling of oil, gas, and geothermal wells, with the primary objective of providing structural support and a seal in the wellbore. It is important to attain adequate cement strength to preserve well integrity, inhibit fluid movement, enhance zonal isolation, and hold the wellbore casing in place. However, a significant challenge arises from the potential contamination of the cement by drilling mud (DM) during the placement process. This study aims to assess the impact of both water-based drilling mud (WBDM) and oil-based drilling mud (OBDM) contamination on the strength of API Class G wellbore cement by comprehensively investigating the underlying causes and the mechanism of strength degradation. Following a systematic methodological approach and the guidelines provided by API and ASTM, cement slurries were prepared and contaminated with DM at 10 %, 20 %, and 30 % concentrations by volume. Subsequently, both neat and contaminated samples were cured for 1, 3, 7, and 14 days, after which a series of uniaxial compressive strength (UCS) tests were conducted, and the results were further reinforced by microstructural and petrophysical property analysis. The results demonstrate that both WBDM and OBDM contamination significantly reduce cement strength, irrespective of curing time. Though the effect of OBDM contamination appears to be more pronounced, the effect of WBDM contamination is also remarkable. After final curing of 14 days, WBDM-contaminated cement samples exhibited an average strength reduction of 45.06 %, while OBDM contamination led to a 66.32 % reduction in strength compared to the neat cement with 30 % contamination. Additionally, for the same curing time and contamination percentage, porosity was found to be increased by 71.18 % and 91.33 %, respectively. The mechanism of neat and contaminated cement hydration is explained with the support of SEM-EDS tests, which confirmed the presence of contamination and revealed how two different drilling muds generate dissimilar pore structures within the cement sheath, affecting the hydration and ultimately contributing to the observed reduction in strength. The findings quantify the negative impact of WBDM and OBDM contamination on wellbore cement, emphasizing the importance of mitigating mud contamination during cementing operations.