A contraction of magotpie, or magata-pie. “Mag” is generally thought to be a contraction of Margaret; thus we have Robin red-breast, Tom-tit, Philip —i.e. a sparrow, etc.
“Augurs and understood relations have (By magotpies, and choughs, and rooks) brought forth The secretst man of blood.”
One's sorrow, two's mirth, Three's a wedding, four's a birth Five's a christening, six a death Seven's heaven, eight is hell, And nine's the devil his ane sel'.
Source: Dictionary of Phrase and Fable, E. Cobham Brewer, 1894
More on Magpie from Infoplease: