Let me repost what I wrote earlier about this....it fits in with the author's overall interest in Jesus as a successor to Moses.
What is interesting about the Magi is that they constitute one out of many links between the Matthean story of the birth of Jesus and OT/haggadaic stories about the birth of Moses. Herod in the gospel narrative takes the place of Pharaoh, and Pharaoh's advisors were identified in midrash as Magi (cf. Philo of Alexandria, Vita Mosis 1.92; b. Sanhedrin 101a; Midrash Rabbah 1.9), and one of these Magi was Balaam son of Beor (cf. Eusebius, Supplementa Quaestionum ad Stephanum; b. Sotah 11a; Sefer Zikhronot 44.9; cf. Diodore of Tarsus on Balaam as an astrologer), who was thought to be the ancestor of all the later Magi (cf. Origen, Homilies on Numbers 13.7; Eusebius, Supplementa Quaestionum ad Stephanum), and whose servants or sons were two other magicians of Pharaoh, Jannes and Jambres (cf. Targum Pseudo-Jonathan Exodus 1:15-16 & Numbers 22:22, Sefer Zikhronot, 45.2, 47.6). The connection with Balaam is fascinating because not only do the Magi bless Jesus in a similar way that Balaam blesses Israel, but Balaam's own blessing prophesied a star and sceptre rising in Israel (Numbers 24:17). This was a popular messianic proof-text of the era (cf. Damascus Document 7:18-26, Testament of Levi 18:3, Josephus, Bellum Judaicum 6.312, and cf. Rabbi Aqiva's naming of Simon bar-Kochba as the messiah) and it also provided the exegetical basis of the star in Matthew's birth narrative (cf. Justin Martyr, Dialogue 106, Irenaeus, Adversus Haereses 3.9.3, Origen, Contra Celsum 1.60).
In the various related traditions about the birth of Moses, (1) Pharaoh receives a disturbing omen in a dream (cf. Targum Pseudo-Jonathan on Exodus 1:15, Midrash ha-Gadol on Exodus 1:22, Sefer Zikhronot, 43.1), (2) Pharaoh's advisors/magicians/Magi interpret the dream and inform him that an Israelite boy would soon be born who would overthrow the power of Egypt (cf. Josephus, Antiquities 2.205, Targum Pseudo-Jonathan on Exodus 1:15, Pirqei R. Eliezer 48, Exodus Rabbah 1.18, Sefer Zikhronot, 43.2), (3) Pharaoh panics and orders all male infants to be put to death and a massacre of Israelite children ensues (cf. Exodus 1:16, Philo, Vita Mosis 1:8, Josephus, Antiquities 2.206, Pseudo-Philo 9:1, Clement of Alexandria, Stromata 1.23.2, Targum Pseudo-Jonathan on Exodus 1:16, Pirqei R. Eliezer 48, Exodus Rabbah 1.18, Sefer Zikhronot, 43.2-3), (4) Moses' parents Amram and Jochebed, along with other Israelite couples, divorce each other so they would not contribute to the massacre (cf. Pseudo-Philo 9:2, Sotah 12a, Midrash ha-Gadol on Deuteronomy 26:7, Baba Batra 60b, Sefer Zikhronot, 44.1), in some versions of the story Jochebed was already pregnant with Moses when Amram divorced her (cf. Sotah 12a, Sekel Tob 2:2), (5) Amram despairs on account of the divorce but he receives a dream vision in which he is told that Jochebed was to give birth to the future deliverer of the Israelites (cf. 4QVision of Amram, Josephus, Antiquities 2.212-215), in some versions of the story it is Miriam that receives the annunciation dream vision or prophecy (cf. Pseudo-Philo 9:10, Megillah 14a, Sotah 12a, Midrash ha-Gadol on Exodus 2:1, Sefer Zikhronot, 43.2), (6) Amram remarries Jochebed privately and in some versions Jochebed was already three months pregnant when Amram took her back (Pseudo-Philo 9:9, Mekhilta de-Rabbi Shimon b. Yohai, Sanya 2.4; Sefer Zikhronot, 44.2), and (7) Jochebed gives birth to Moses, and in some versions of the story the magicians/Magi unwittingly fail to help Pharaoh in discovering the child (Josephus, Antiquities 2.218, Sotah 12b, Pirqei de R. Eliezer 48, Sefer Zikhronot, 44.2). There are three main differences between these traditions and the story in Matthew: the omen being a star leading the Magi to Jesus instead of a dream, the reason for the divorce, and the reversed order between divorce and massacre. The motif of the star appears not in Moses haggadah but in messianic interpretations of the Balaam oracle in Numbers 24:17, but the identification of Balaam as one of the magicians of Pharaoh and as the founder of the Magi gives one plausible reason for a relationship between the messianic star motif and the nativity traditions of Moses. The theme of the star going before to Magi, leading them to Jesus has another parallel with the story of Moses, namely, the pillar of cloud and fire that went before the Israelites to lead them out of Egypt (Exodus 13:21-22, 14:24). The novel reason for the divorce probably incorporates a piece of independent Jesus tradition responding to the claim that Mary's pregnancy as illegitimate (already echoed in the genealogy in Matthew 1:3, 5-6 and attested in the "Pandera" tradition in Origin, Contra Celsum 1.28-33, Epiphanius, Adversus Haereses 3.78.7, b. Yebamoth 49a). This different reason for the divorce also allows the divorce to come before the massacre, whereas in the Moses haggadah the divorce is a consequence of Pharaoh's decree.
The material that follows the story of the Magi in Matthew 2:1-12 no longer parallels midrash on Moses' birth and instead takes up material pertaining to Moses and the exodus from the OT itself: (8) in v. 13-14 Jesus taken from his homeland because Herod endeavored to kill him, just as Moses fled from the land of his birth because Pharaoh sought to kill him (cf. Exodus 2:15), (9) v. 14-15 is also closely paralleled by 1 Kings 11:40, with Jeroboam fleeing to Egypt to escape from Solomon and staying there until Solomon's death, (10) the citation of Hosea 11:1 in v. 15 reflects the tradition of the Israelite exodus from Egypt in that text, and the author was probably led to that text by Balaam's oracle in Numbers 24:8 which has similar wording, (11) the use of teleuté to refer to the death of Herod in v. 15 is a hapax legomenon in the NT but reflects the wording in Exodus 4:19 LXX: "the king of Egypt died (eteleutésen)", (12) the massacre of male children related in v. 16 pertains to both the massacre of male children in Exodus 1:22 as well as Pharaoh seeking to kill Moses specifically in Exodus 2:15, (13) the command to Joseph to return to the land of his birth in v. 20 reproduces almost verbatim the text of Exodus 4:19 LXX (M: tethnékasin gar hoi zétountes tén psukhén tou paidiou; LXX: tethnékasin gar pantes hoi zétountes sou tén psukhén), even incorporating the plural hoi zétountes even though it has only a singular "Herod" as its grammatical antecedent, and (14) in v. 21 Joseph took his son and wife and returned to Israel, just as Moses took his wife and sons and returned to Egypt in Exodus 4:20.
One interesting property of the Matthean parallels to Moses traditions is their absence from the Lukan account of the birth of Jesus. The Magi, the star associated with the Magi, King Herod panicking at ominous news and desiring to kill the newborn threatening his rulership, the divorcing between Joseph and Mary, the massacre of the infants, the flight from Egypt, and the call to return to Egypt -- these elements are wholly absent in Luke's story. This can be simply accounted for by supposing that the author of Matthew drew on Moses traditions whereas the author of Luke did not.
The narrative focus on Moses in Matthew may reflect the author's interest in portraying Jesus as the fulfiller of the Law, as the rabbi who finally delivers the true understanding of the Torah (cf. especially ch. 5 and 23), thus making him a natural counterpart of Moses.
The star accompanying Mary's pregnancy is related in Ignatius, Ephesians 19:2-3 in rather mythological terms (the star outshining all others and leading other stars together with the sun and moon to circle around it in a chorus, cf. the variant account in the Protevangelium of James 21:2) as occurring at the moment when the evil heavenly aeons were thrown into utter disarray -- this hints at a Chaoskampf theme in which the power of evil is defeated by divine providence. In Revelation 12, we have the birth of the messiah child occuring at the same time "a great and wondrous sign appeared in heaven" (v. 1), and the pregnant mother of the messiah is described in astral terms with "a crown of twelve stars on her head" with the sun and moon, paralleling the chorus of sun, moon, and stars in Ignatius (and cf. Isis as the "queen of heaven" associated with the sun, moon, and stars). But also interesting is the fact that the birth of the messiah child is threatened by the Dragon (= the Devil) who wants to kill the child "the moment it was born" (v. 5), and then the child was snatched into safety into God's presence and the mother "fled into the desert to a place prepared for her by God" (v. 6). This constitutes a strong parallel with Matthew's plot of Herod endeavoring to kill Jesus and Mary fleeing with the child into Egypt (= the desert). The same story also relates a classic Chaoskampf battle between the angels of heaven (= the aeons of Ignatius) and the Dragon, with the Dragon defeated at the time the child's mother fled into the desert with the help of an eagle (v. 7-9, 13-14). The seemingly independent myth about the coming of the Savior in the Apocalypse of Adam parallels both Revelation and Matthew, without an overt Chaoskampf motif: "He came from a virgin womb. He was cast out of his city, he and his mother; he was brought to a desert place... And a bird came, took the child who was born and brought him onto a high mountain. And he was nourished by the bird of heaven. An angel came forth there and said to him, 'Arise, God has given you glory,' and thus he received glory and power" (78:6-26). So it is possible that the plot of sign in heaven during birth pangs > virgin birth > child threatened by forces of evil > child and mother escape to a desert place is an older mytheme that has different expressions in Revelation, Ignatius, the Apocalypse of Adam, and Matthew -- where in the case of the latter Herod takes the role that the Devil has in Revelation 12. But what is interesting about the myth in Revelation 12 is that it shows multiple parallels with the Egyptian myth of Horus Harpocrates-Isis-Typhon and the Greek myth of Apollo-Leto-Python. Herodotus (Historiae 2.156) in the fifth century BC showed that the Greek myth was a version of the Egyptian one, and the story was elsewhere related in variant forms in the Metternich Stele, Plutarch, De Iside 355-358, Lucan, Pharsalia 5.80-98, and Hyginus, Fabulae 140. In these stories, Leto/Isis -- pregnant by Zeus/Osiris -- was pursued by the great dragon Python/Typhon because he has learned that she would bear a child who would kill him, and she flees to a secret place with the help of the south wind (cf. the similar role of the wings of an eagle in Revelation) and the god Poseidon, and she gives birth to Apollo/Horus who then returns and defeats Python/Typhon and takes his authority to rule as king. It is certainly unclear whether the Leto/Isis myth underlies the whole birth > threat > flight plot pertaining to the birth of Jesus, since John of Patmos could have secondarily employed it in Revelation (along with some influence from Genesis 2-3) just as the author of Matthew secondarily appealed to traditions of the birth of Moses. But since the story in Matthew is so interested in Egypt both in terms of Moses and in terms of Jesus fleeing there, it is possible that the Egyptian myth (or some Hellenistic version of it) is not irrelevant to the Matthean story but played some role in the development of its underlying traditions.
What is interesting about Numbers 24:17 is that it looked to a messiah coming from within Israel whereas another messianic proof-text, Genesis 49:10 (in conjunction with Daniel 9:26) was used by the Herodian party to legitimize Herod as a Gentile messiah coming from outside Israel (cf. Epiphanius, Adversus Haereses 1.20.1.6, Origen, Commentary on Matthew 40, Eusebius, Demonstratio Evangelica 8.2.37, 8.2.102-103, Historia Ecclesiastica 1.6.5, Slavonic Josephus, Bellum Judaica 1.364-370). If it was Herodian propaganda to point to Genesis 49:10 and Daniel 9:26 to justify a Gentile ruler over Judea, the concurrent popularity of Numbers 24:17 (a prophecy made by a Gentile no less) as a messianic oracle may reflect its use as anti-Herodian propaganda by the king's political adversaries. There is also an interesting resonance with the story in Matthew 2 and what the historical Herod himself experienced. In 41 BC, the king of the Parthians (whose advisors included the Magi) came from the East and invaded Judea and overthrew the ruling priest-king Hyrcanus who was a puppet of the Romans. Herod, who was a loyal ally of Octavian (Augustus Caesar) and Mark Antony, fled Judea and escaped to Egypt and Queen Cleopatra arranged for him to meet the emperor in Rome. After a short exile, Herod received Roman backing and was made king of Judea in 40 BC and with the help of Roman legions he returned to Judea and fought against anti-Roman Jews loyal to the Parthians, finally ousting the Parthians in 38 BC but only through a bloody massacre was he able to secure Jerusalem and the throne of Judea in the summer of 37 BC. So in his rise to power, Herod experienced the arrival of Pathians from the East, a flight from Judea to Egypt followed by a return, and a grim massacre of the population at his own hands. Could there be a propagandistic reflection of these events in the story in Matthew? Maybe.
Well, there certainly was a lot of interest and influence in many specific cases. With respect to Mesopotamia, we can see its clear imprint in the primeval narratives of Genesis (with parallels to the Gilgamesh Epic and the Enuma Elish) and later Enochic literature (with even "Gilgamesh" appearing as a character in the Book of Giants). With respect to Egypt, we can see the relationship between Psalm 104 with the Hymn to the Aten and Proverbs 22-23 with the Instruction of Amenemope. With respect to Persia, we can see the influence of Zoroastrianism on Jewish dualism and apocalypticism, and even a Zoroastrian text (the Oracles of Hystaspes) circulating in a Jewish redaction in Jewish and Christian circles (which was likely a source for Revelation). Similarly, we can observe how Jews and Christians wrote oracles under the guise of the Sibyl. And of course Hellenistic thought deeply entrenched itself in the later Second Temple period.