Matthew 2:19

But when Herod was dead, behold, an angel of the Lord appeareth in a dream to Joseph in Egypt,

After Herod died, while Joseph was still in Egypt, an angel of the Lord appeared to him in a dream and directed him to return to Israel with the child and his mother. The same appearing was a warning to leave Egypt now that return had become possible. Herod died miserably, unmourned by his family and by his subjects. His death ended the immediate danger to Jesus, because those who had sought to take the child's life were dead.

Joseph intended to settle in Bethlehem in Judah, because he regarded Jesus as heir of David. But he was afraid to go into Judea because Herod's son Archelaus now ruled there in his father's place. Archelaus had begun his rule with violence, and Joseph feared he would continue his father's plan to kill Jesus.

God therefore guided the family away from Judea to safety, directing them to Nazareth. Joseph learned of Archelaus's rule and feared to enter his territory, so by divine direction through the angel he turned aside to the safer place. This protection and guidance in the return and resettling was part of God's care for Jesus until His saving mission was completed.