This nickname for people peddling fake cures and/or pretending to have medical skills they don’t actually possess has been around since at least the early 17th century. The Oxford English Dictionary lists the oldest recorded use in Francis Quarles’ 1638 book, Hieroglyphikes of the Life of Man: “Quack, leave thy trade; thy dealings are not right, thou tak’st our weighty gold, to give us light.”
Quack, in the sense of a medical impostor, is a shortening of the old Dutch quacksalver (spelled kwakzalver in the modern Dutch), which originally meant a person who cures with home remedies, and then came to mean one using false cures or knowledge.
The etymological trail gets muddy if you go any further back than that, and quacksalver has been traced variously to kwakken (to fling or throw down) + zalver (person who cures with ointments), quacsalven (a term for home remedies, from the 1300s), and quaken (to quack or croak) + salf (salve). Taken figuratively, the kwakken and quaken origins imply someone peddling and boasting about their medical wares, legitimate or not—a little broader, but not too far off from the modern usage.
Quacksalver’s similarity to quicksilver, or mercury—a heavy metal sometimes slipped into counterfeit medications—might suggest a connection between the two, but there doesn't seem to be any recorded evidence that the element led to the name.