Q. Given that Jesus was consecrated at the temple in Jerusalem on the 40th day and afterwards Mary and Joseph returned to Nazareth, doesn’t that mean that the wise men visited Bethlehem prior to Jesus’ 40th day?
Actually, the wise men did not visit Jesus in Bethlehem. When Herod asked them when they had seen the star that led them to come look for Jesus, they told him it had appeared two years before. (That is why, tragically, Herod ordered his soldiers to kill all of the boys in the area of Bethlehem who were two years old or younger.) Mary and Joseph only went to Bethlehem for the census, and as you observe, they returned to their home in Nazareth after forty days, after they had “done everything required by the Law of the Lord.” The account of the wise men in the Gospel of Matthew says that “the star they had seen when it rose went ahead of them until it stopped over the place where the child was. When they saw the star, they were overjoyed. On coming to the house, they saw the child with his mother Mary, and they bowed down and worshiped him.” So Mary, Joseph, and Jesus were now in a “house,” no longer in the improvised situation in crowded Bethlehem that had required Mary to lay the newborn Jesus in a manger.
You may have been asking about the timing of the wise men’s visit because Matthew says that they came to see Jesus two years after he was born, but Luke indicates that his parents were only in Bethlehem for forty days after he was born. The solution to this difficulty is that the wise men did not visit Jesus in Bethlehem. While Matthew says that Herod “sent them to Bethlehem” on the basis of what the priests and teachers of the law told him from Micah’s prophecy, Jesus was no longer there. The star guided them to the right place.