Joseph was ready to ‘divorce’ Mary because of her ‘news’.
“But when he had thought this over, behold, an angel of the Lord appeared to him in a dream, saying, “Joseph, son of David, do not be afraid to take Mary as your wife; for the child who has been conceived in her is of the Holy Spirit.”
This may once again have been Gabriel, who never announced who he was to Mary and then only chose to announce himself to Zechariah when it became clear that it was necessary.
The angel appeared to him in a dream and shared with Joseph…
“She will give birth to a son, and you are to name Him Jesus, because He will save His people from their sins.”
This exactly mirrors what Gabriel said to Mary, and what Mary likely shared with Joseph:
“You will conceive and give birth to a son, and you are to give him the name Jesus”
Joseph was a righteous man and likely cried out to God for wisdom (James 1:5) when he heard from Mary that she was with child. What Joseph heard from the Angel was a clear confirmation of what had already been shared with him by Mary. It was God’s Grace to help him move forward in faith. This was more than a strange dream but was Joseph ‘hearing from God’ so that now he could move forward with Mary.