Greetings in that Divine and Most Precious Name of Our Lord and Savior Jesus Christ!
The Gospel of Luke says that Mary and Joseph lived in Nazareth at the the time of the annunication (Lk. 1:26) and during part of Mary's pregnancy with Jesus (Lk. 2:4); it also says they returned there later after Jesus was born (Lk. 2:39). However, in the Gospel of Matthew there is a line which seems to imply that Joseph and Mary were not living in Nazareth until after Jesus was born and they returned from Egypt:
"And [Joseph] arose, and took the young child and his mother, and came into the land of Israel. But when he heard that Archelaus did reign in Judea in the room of his father Herod, he was afraid to go thither, notwithstanding, being warned of God in a dream, he turned aside into the parts of Galilee, and he came and dwelt in a city called Nazareth" (Matt. 2:21-23 KJV).
"So he got up, took the child and his mother and went to the land of Israel. But when he heard that Archelaus was reigning in Judea in place of his father Herod, he was afraid to go there. Having been warned in a dream, he withdrew to the district of Galilee, and he went and lived in a town called Nazareth." (Matt. 2:21-23 NIV)
If they had been living in Nazareth before, then why mention them not going to where Herod's son was because they were afraid? Is the Gospel just saying that they would have went to where Herod's son was ruling rather than back to Nazareth, had they been able, but since they feared for their lives settled for the second option of returning to Nazareth? Or is there some other answer that I'm not seeing?
The Gospels are Synoptic, they are different viewpoints of the same events.
Luke emphasizes the background as to how and why Joseph and Mary are in Nazareth, Matthew emphasizes the miraculous vision as to how they returned to Nazareth. Matthew is not saying one way or another the connection which Joseph has with Nazareth, whereas Luke is telling us this history.The way to connect the gap is to say, "he went and lived a town called Nazareth (Matthew 2:23), to their own town (Luke 2:39)."
In this way we understand that the Holy Family returned to Nazareth where they initially lived.