Thermal treatment has significant advantages in resource recovery for oily sludge (OS). However, the instability of heavy metals (HMs) within the residue poses a considerable risk of secondary pollution. This study explored the migration and transformation of HMs from OS under varying conditions (i.e., temperature, constant-temperature duration time, and different ratios of O2 and CO2). The elevation of the pyrolysis temperature augmented the decomposition of organic matter and total petroleum hydrocarbons (TPHs). However, the increased temperature also diminished the stabilization of HMs, and facilitating the HM’s transfer to oil and gas, particularly for HMs (i.e., As and Pb) with low boiling points. The constant-temperature duration time exhibited a weak impact on HM transformation, but the internal heating mechanism of microwave pyrolysis promoted the stabilization of HMs through vitrification. The existing O2 with oxidizing properties facilitated the oxidation of organic matter and TPHs to CO2 and H2O, which also promoted the transformation of HMs into oxidized states for stabilization. Comparatively, CO2 promoted the thermal cracking and disrupted the stability of HMs to a certain extent. Above all, this work revealed the migration and transformation of HMs in OS varied with the thermochemical methods and possessed an important significance for the immobilization and stabilization of HMs.