Soil-borne heavy metals are an increasing problem due to contamination from human sources and can enter the food chain by being taken up by plants. Understanding the genetic basis of accumulation and tolerance in plants is therefore important for reducing the uptake of toxic metals in crops and crop relatives, as well as for removing heavy metals from soils by means of phytoremediation. Legumes contain important crop species and have developed symbiosis with nitrogen fixing bacteria. In this study, 220 accessions from the HapMap collection of the model legume Medicago truncatula were analyzed for cadmium (Cd) and mercury (Hg) tolerance and accumulation. Accumulation of Cd and Hg was measured using ionomics in the leaves, and relative root growth was used as an indicator for tolerance. A large variation was found in all traits, especially Hg leaf accumulation, with some individuals showing very high leaf cadmium levels. A positive correlation between Cd and Hg relative root growth was observed, while no correlation was found for accumulation in the leaves. To identify genes and genomic regions involved in Cd and Hg tolerance and accumulation, a genome-wide association study (GWAS) was performed. These phenotypes were found to be complex, polygenic traits, and among the genes detected by GWAS many were conserved in other species and many new candidate genes were identified. An interesting region on chromosome 2 contained several ankyrin repeat genes significantly associated with Cd tolerance and was near a genomic region shown to be associated with salinity stress response in M. truncatula, demonstrating that this region is enriched in genes involved in a ion stress response. By grouping plant genotypes with contrasting phenotypes, regions of genomic divergence were identified containing several gene ontologies relevant for metal transport and stress response. The tests of genomic divergence identified candidate genes which were not found by GWAS and could therefore be a promising approach to complement GWAS. The significant variants identified by GWAS showed a negative correlation between minor allele frequency and effect size for Cd tolerance and accumulation, with large effect alleles being the most rare. This pattern is consistent with mutation-selection balance. In conclusion, this study identified potential molecular mechanisms involved in Cd and Hg tolerance and accumulation. These findings may help to understand the genetic interactions between host plants and symbiotic rhizobia in the presence of toxic heavy metals.