There's an interesting post about Matthew 2:13-15 here in the Naked Bible: http://michaelsheiser.com/TheNakedBible/2011/08/were-new-testament-writers-hermeneutical-hacks/ . I like a lot of what he says, but I differ on one point; I don't think Matthew got the story from Mary. I think he made it up.
There's a progression in the New Testament writers. Paul thinks that Jesus is 'declared to be son of God with power' at his resurrection (Romans 1:4); he's exalted and given the 'name above all names', ie God's name (Philippians 2:9), and he really isn't interested in the unexalted, unresurrected Jesus who the disciples knew. Mark is, however. He thinks Jesus was adopted as God's son at his baptism (Mark 1:11), and he's not interested in his life before this event. Matthew, however, thought he was God's son from birth. He probably didn't know anything about his early years - he and Luke tell incompatible stories, and manage to come up with dates ten years or so apart for the Nativity - and the story he does tell is essentially theological. It wasn't, of course, a particularly literate society, and peasants have often been vague about exactly when they were born.
Jesus is born miraculously, like Isaac or Samuel, except that this time the birth happens to a young woman, not an old one. Doubtless Mary was known to have been young at the time; we don't know when she died, but her son James ran the Jerusalem church for a generation. The rough outline, at least, would have been known. Jesus' Messiahship is testified to by the star, based on Balaam's star prophecy in Numbers 24.
Matthew is writing for devout Jews, who don't like the Pharisees - his Jesus slags them off a lot more than anyone else's - but seem to have followed their interpretations of the Law. He's keen to emphasise that Gentiles have their place in the Kingdom, and so the first people to honour the infant Messiah are, of course, some rather learned Gentiles. The Jews reject Jesus, and Matthew blames them for his death (27:23; this isn't antisemitic, since it's written by a Jew. It has, of course, been used in an antisemitic way subsequently). So Jesus is on the recieving end of an assassination attempt by a sort-of-Jewish king.
Matthew wants to portray Jesus, among other things, as the prophet like Moses promised in Deuteronomy 18:15-19. This becomes most obvious when he has Jesus stand on a mountain to deliver his interpretation of the Law. Here, he takes the opportunity to place him in Egypt - doubtless he has in mind that Moses was himself a refugee, though it was in Midian not Egypt - and then has God call him back to his own people. No doubt the thought that God's son Israel also came out of Egypt wasn't far from the back of his mind; why confine yourself to one implied reference when you can manage two at once?
What we have here is a narrative which foreshadows the story Matthew is about to tell, and which at the same time lets us know who he believes Jesus to have been.