Mary, born at
St James's Palace in London on 30 April 1662, was the eldest daughter of the Duke of York (the future
King James II & VII), and his first wife,
Anne Hyde. Mary's uncle was
King Charles II, who ruled the three kingdoms of
Ireland; her maternal grandfather,
Edward Hyde, 1st Earl of Clarendon, served for a lengthy period as Charles's chief advisor. She was baptised into the
Anglican faith in the
Chapel Royal at St James's, and was named after her ancestor,
Mary, Queen of Scots. Her godparents included her father's cousin,
Prince Rupert of the Rhine.
 Although her mother bore eight children, all except Mary and her younger sister
Anne died very young, and King Charles II had no legitimate children. Consequently, for most of her childhood, Mary was second in line to the throne after her father.
The Duke of York converted to Roman Catholicism in 1668 or 1669 and the Duchess about eight years earlier, but Mary and Anne were brought up as Anglicans, pursuant to the command of Charles II.
 They were moved to their own establishment at
Richmond Palace, where they were raised by their governess
Lady Frances Villiers, with only occasional visits to see their parents at St James's or their grandfather Lord Clarendon at
 Mary's education, from private tutors, was largely restricted to music, dance, drawing, French, and religious instruction.
 Her mother died in 1671, and her father remarried in 1673, taking as his second wife
Mary of Modena, a Catholic who was only four years older than Mary.
From about the age of nine until her marriage, Mary wrote passionate letters to an older girl,
Frances Apsley, the daughter of courtier
Sir Allen Apsley. In time, Frances became uncomfortable with the correspondence,
 and replied more formally. At the age of fifteen, Mary became betrothed to her cousin, the Protestant
William III of Orange. William was the son of the King's late sister,
Mary, Princess Royal, and thus fourth in the line of succession after James, Mary, and Anne.
 At first, Charles II opposed the alliance with the Dutch ruler—he preferred that Mary wed the heir to the French throne, the
Dauphin Louis, thus allying his realms with Catholic France and strengthening the odds of an eventual Catholic successor in Britain; but later, under pressure from Parliament and with a coalition with the Catholic French no longer politically favourable, he approved the proposed union.
 The Duke of York agreed to the marriage, after pressure from chief minister
Lord Danby and the King, who incorrectly assumed that it would improve James's popularity among Protestants.
 When James told Mary that she was to marry her cousin, "she wept all that afternoon and all the following day".