With Silver Bells and cockle shells and pretty maids all in a row.
The origin of this nursery rhyme is not known. The oldest known print version is from Tommy Thumbs pretty song book published in 1744.
With the invention of the printing press, It was becoming much easier to print and circulate stories and rhymes that had only been spoken or hand-printed over many years, even centuries. It was a time which became known in the 18th century as the, “Age of Enlightenment”. As more printing presses were produced, more information was published at prices that the general public could afford. More people learned to read. Its not surprising to see how variations on the old traditional nursery rhymes might sell more books. Especially if they involved scandal of royalty or public figures. Both Mary I of England, also known as Bloody Mary, and Mary Queen of Scots, are thought to be inspirations for the rhyme. To learn more about this, go to the Wikipedia page Mary Mary Quite Contrary.