“And when they had completed everything required by the law of the Lord.” This is one of the very many places where the word “and” does not indicate that the two events connected by the “and” happened in immediate succession; the “and” simply continues the narration. Another thing that had to be completed after the offering for sin was Jesus’ trip to Egypt. When we put the events of the other Gospels together with this verse, we can see that Joseph and Mary stayed in Bethlehem after Jesus was born until they went to Egypt, and they went from there to Nazareth.
Jesus was born in Bethlehem (Luke 2:4, 11). Joseph and his family still would have been there 40 days later when they had to travel the seven miles (11.2 km) north to Jerusalem to present Jesus in the Temple and offer a sacrifice (Luke 2:21-24; Lev. 12:1-8). They were still in Bethlehem when the Magi arrived 18 months to two years later (Matt. 2:8). There simply is no evidence that they left Bethlehem and went back to Nazareth then went back to Bethlehem again in that time, and no reason for them to have done so. After the Magi left Bethlehem, Joseph took Mary and Jesus and went to Egypt (Matt. 2:14). He stayed there until Herod was dead and then went back to Nazareth (Matt. 2:15, 23).
A major key to the chronology of the birth of Christ is the phrase in this verse, “when they had completed everything required by the law of the Lord.” The word “law” is nomos (#3551 νόμος), and it has a very wide semantic range. The meanings of nomos include anything established, such as a custom; a law, rule, regulation, principle, precept, or injunction; the Mosaic Law; the entire Old Testament; and the moral instruction given by Christ. The exact meaning of nomos in any given context must be determined from that context. In this context, nomos refers to the entire Old Testament, and thus “everything required by the law” refers to all the things in the Old Testament spoken about the Christ, whether it was in the Torah (the five books of Moses), the prophets, or the writings. In this context, the Greek phrase kata ho nomos, often translated as “according to the law,” refers to the things in the law, or things required by the law (cp. BBE, CEB, CJB, NIV, NJB, NLT, NRSV, and see commentary on Gal. 3:29). Part of what was in the law that had to be fulfilled by the Messiah was that he had to be called out from Egypt (Matt. 2:15; Hos. 11:1). Remembering that, we can see that Luke 2:39 is a kind of summary verse that mentions the trip to Egypt simply by saying they fulfilled everything required by the law.
In summary, Jesus was born in Bethlehem, and his parents stayed there for perhaps as long as two years after he was born, then they went to Egypt to escape Herod. They left Egypt when Herod was dead, and settled in Nazareth.