To assess contamination levels and ecological risks of heavy metals in agricultural soil from Shanxi Province of China, a total of 33 samples in the surface soil were collected from 11 cities in Shanxi. The soil samples were digested by a mixed acid of nitric acid and hydrofluoric acid on a microwave digestion system, then the levels of eight heavy metals were analysed using an inductively coupled plasma mass spectrometer. The pollution levels of soil heavy metals were evaluated using a geo-accumulation index and their ecological risks were assessed using risk index calculated by Hakanson's method. As a result, the average concentrations of the heavy metals As, Cd, Cr, Cu, Hg, Ni, Pb and Zn were 12.9 ± 4.8, 0.35 ± 0.23, 43 ± 14, 27 ± 19, 0.25 ± 0.14, 21.7 ± 5.7, 17 ± 13 and 89 ± 53 mg kg
−1
, respectively. By comparison to the
Chinese soil environmental quality
(GB15618-2018), only 9% of Cd samples and 3% of Cu samples exceeded their corresponding screening criteria. Subsequently, the results of geo-accumulation indices suggested that Shanxi's soil suffered from moderate to heavy contamination posed by Cd and Hg, and risk indices exhibited a similar trend that Cd and Hg were the main contributors for considerable to very high ecological risk. Finally, the analysis of variance indicated that the mean levels of Cd significantly occurred at Yuncheng areas among the 11 cities (
n
= 3,
p
< 0.05), but Hg concentrations did not have significantly statistical differences. This study demonstrated that metals Cd and Hg had higher levels and ecological risks for agricultural soil in Shanxi, especially, Yuncheng City suffered from heavy Cd contamination. The findings of the present study will provide basic information on management and control of the agricultural soil contamination in Shanxi Province, China.