For what they're worth:
The Joan Baez songbook, crediting Child Ballad #173, for the song "Mary Hamilton," says: "The ballad tale told here bears resemblance to two distinct historical occurrences: one relating to a 16th century incident in the court of Mary Queen of Scots, and the other to an affair in the court of Russia's Czar Peter in the 18th century. At this late date, however, oral tradition has altered the story too greatly to pinpoint the exact incident on which the ballad might have been based. The long circumstantial version given here does not have much currency today among traditionak singers; all that usually remains is a lyric lament in which Mary Hamilton makes a farewell swpeech without any explanation of why she is being punished." The verses, however, DO indicate that the "crime" was that of killing her "own wee babe.")
In "The Viking Book of Folk Ballads of the English-Speaking World" (Ed. by Albert B. Friedman, 1956)contains two Mary Hmilton ballads. The intro for #173, "Mary Hamilton" (p.183) says: "The search for the historical Mary Hamilton has proved tantlizing but elusive. There was a circle of ladies-in-waiting to Mary, Queen of Scots, popularly called 'the four Maries,' but a Mary Hamilton was not among them. Her crime and punishment, however, parallel a scandal of Mary's reign involving a French attendant who was executed for murdering her newborn child. Not Darnley, 'the highest Stewart of a',' but the Queen's apothecary, the highest steward, was the French woman's accomplice in love and crime. This was in 1563.
In 1719 a beautiful Scottish maid-of-honor at Peter The Great's court
named Mary Hamilton was beheaded for infanticide. Other circumstances in the Russian case beside the name tally with the ballad: the girl, for one thing, refused to wear sober clothes to the scaffold. Also, her lover was a high-born courtier. One would be tempted to consider the ballad an outgrowth of the Russian tragedy of 1719 if it were not for the troublesome fact that some form of the ballad seems to heve been circulated in Scotland before 1719. This older form was probably a ballad in which the Frenchwoman's crime was foisted upon one of the four Maries. Perhaps such a connection arose because of the common use of 'mary' in Scotland for servant maid. In fact there is a version of 'Mary Hamilton' (see Child, IV 509) in which the girl is simply 'Marie' and her lover is a 'pottinger,' the court apothecary of the criminal records. Apparently the news from St. Petersburg and the real Mary Hamilton's jaunty demeanor caught the Scottish imagination and the old ballad was revamped to suit a new 'heroine.'"
On page 219 in a section titled Criminals' Goodnights is a ballad called "Mary Hamilton's Last Goodnight." The note says, " The old Scottish ballad of 'Mary Hamilton' tells the pathetic story of an unwed lady-in-waiting to Mary, Queen of Scots, who murdered the child she had borne as the result of an amorous intrigue with a courtier -- in somw versions the seducer is Lord Darnley, the Queen's husband. She is sentenced to hang for her crime. Several American versions of 'Mary Hamilton' hardly even allude to the seduction or murder, probably out of prudery, but perhaps simply to gain sympathy for the 'heroine, and reduce the ballad to the touching lament which the condemned woman makes on the scaffold. This form of 'Mary Hamilton' furnishes us with a folk parallel for the broadside goodnight."
Finally, in the collection of Francis James Child of "English and Scottish Popular Ballads, (edited by Helen Child Sargent and George Lyman Kittredge), 1904, is this notation for Ballad # 173, "Mary Hamilton." "When Mary Stuart was sent to France in 1548, being then between 5 and 6 (years old), ashe had four companions, 'sundry gentlewomen and noblemen's sons and daughters , almost of her own age, of which there were four in special of whom every one of them bore the same name of Mary, being of four sundry honorable houses, to wit, Fleming, Livingston, Seton, and Beaton of Creich; who remained all four with the queen in France during her residence there, and returned again in Scotland with her Majesty in the year of our Lord 1561.'
"This ballad purports to relate the tragic history of one of queen's Maries. In some of the versions her lover is said to be the king (Darnley). The ballad seems to have taken its rise in an incident which occurred at Mary's court in 1563, which involved the queen's apothecary and 'a French woman that served in the Queen's chamber.' There is also a striking coincidence between the ballad and the fate of a Miss Hamiloton who, in the reign of Peter the Great, was one of the attendants of the Russian Empress." There are two versions of the ballad "Mary Hamilton" that follow.
I think it's safe to say that there are no simple answers as to the origin and/or subjects of the ballad or ballads. The answer to the original question is clearly explained by Malcolm Douglas in the 3rd post on this thread and, perhaps, should have stopped there. My post here probably involves far more information and speculation than anyone reading this really wanted to know, but I had some time on my hands so there you are. One thing I think, is clear and that is that there ARE historical roots (more than one root, it seems) for the ballad.