After the scholars were gone, God's angel showed up again in Joseph's dream and commanded, "Get up. Take the child and his mother and flee to Egypt. Stay until further notice. Herod is on the hunt for this child, and wants to kill him."
Joseph obeyed. He got up, took the child and his mother under cover of darkness. They were out of town and well on their way by daylight.
They lived in Egypt until Herod's death. This Egyptian exile fulfilled what Hosea had preached: "I called my son out of Egypt."
Herod, when he realized that the scholars had tricked him, flew into a rage. He commanded the murder of every little boy two years old and under who lived in Bethlehem and its surrounding hills. (He determined that age from information he'd gotten from the scholars.)
That's when Jeremiah's sermon was fulfilled:
A sound was heard in Ramah, weeping and much lament. Rachel weeping for her children, Rachel refusing all solace, Her children gone, dead and buried.
Later, when Herod died, God's angel appeared in a dream to Joseph in Egypt:
"Up, take the child and his mother and return to Israel. All those out to murder the child are dead."
Joseph obeyed. He got up, took the child and his mother, and reentered Israel.
When he heard, though, that Archelaus had succeeded his father, Herod, as king in Judea, he was afraid to go there. But then Joseph was directed in a dream to go to the hills of Galilee.
On arrival, he settled in the village of Nazareth. This move was a fulfillment of the prophetic words, "He shall be called a Nazarene."