I'm curious about monarchs and inheritance of the throne beyond this specific example as well. For example, had a King died when his expectant Queen was, say, 5 months pregnant with the first child/boy, would they have waited until it was born, or would someone else automatically inherit because there is no baby yet?
Just speaking in general terms, there have been at least two posthumous kings in European history, where their predecessor died while they were still in utero. John I of France was born 5 months after the death of Louis X in 1316 and the crown was held in regency until the birth. He only lived for five days, but was king during that entire time.
The other example that comes to mind is Ladislaus the Posthumous, an Archduke of Austria and King of Hungary in the 15th century, born four months after the death of his father. His father left him all of his titles, but while the Austrian nobles accepted him, the Hungarians crowned Władysław III of Poland as King of Hungary instead. There was a bit of a civil war but Władysław died four years later anyway, so Ladislaus ended up inheriting that title at the age of 4. There was additional power-wrangling over who would get to be his guardian/regent, but that's outside the scope of this question.
Presumably, the Scottish would have had similar reactions to the situation, but one can imagine at least some among the nobility may have wanted to go for an alternative.
A question that I can't answer but would be an interesting hypothetical followup: If a crown were inherited in an agnatic-cognatic manner, with all daughters no matter how old following all sons, and a king died with only a daughter and a pregnant wife, who would inherit? He had a clear heir at death, but if the wife gave birth to a boy, his claims would supercede his sisters. That said, I am not aware of it ever happening (at least in Europe), but I've wondered that one before. John I of France is the closest it ever happened as he did have an older sister, but a woman had never inherited the French throne so no one was quite sure of her claims. After his death, his Uncle took power and the French later formalized true Salic law (formally excluding all claimaints via female-line descent).
In such a scenario, someone would act as Regent for the child. A Regent is someone who handles the duties and roles when the heir to the throne is too young or mentally incapacitated. Typically, a Regent is the closest relative like their mother, uncle, or grandparent, a religious leader, or someone of power. In the case of Mary, whose father died when she was born, she had regents who ran Scotland in her name. Her first Regent was James Hamilton, Duke of Arran, who was then replaced by Mary's mother, Mary of Guise. Had Mary died in womb, James Hamilton, Duke of Arran, would have inherited the throne because he was the next in line through his lineage from his ancestor, James II