This study presents a comparative analysis of friction stir welding (FSW) and underwater friction stir welding (UFSW) of AZ31 magnesium alloy in T-configuration, emphasizing the effects on heat distribution, material properties, and mechanical performance. Simulation results revealed a more uniform heat distribution in both welding techniques, with the hottest area on the advancing side. The maximum temperatures recorded at the shoulder-workpiece contact were 404 °C for FSW and 349 °C for UFSW, a 13.6 % reduction in UFSW. Material velocity at the trailing edge was 63 mm/s for FSW and 42 mm/s for UFSW, showing a 34 % decrease due to lower heat generation in UFSW. Strain rates were 450 s⁻¹ for FSW and 420 s⁻¹ for UFSW. Grain size in the stir zone was 26 micrometers for FSW and 21 micrometers for UFSW, a 19 % reduction. Ultimate tensile strength (UTS) increased by 6 % in the skin direction and 12.8 % in the flange direction for UFSW compared to FSW. SEM analysis indicated enhanced ductility in UFSW fractures. These results demonstrate UFSW's superiority in improving thermal management, microstructural properties, and mechanical performance of welded joints.