If we look at the part from Matthew, it is clear that Mary knew she was pregnant, or was getting pregnant before Joseph did. Joseph thinks about putting her away before he has a dream where he is told to stay with her. Matthew 1:19:
19 Then Joseph her husband, being a just man, and not willing to make
her a public example, was minded to put her away privily
Joseph is thinking of putting her away because he is a just man, he knows the scripture that says in Deteronomy 22:22-27 that, at least Mary, is in trouble.
22 If a man be found lying with a woman married to an husband, then
they shall both of them die, both the man that lay with the woman, and
the woman: so shalt thou put away evil from Israel.
23 If a damsel that is a virgin be betrothed unto an husband, and a
man find her in the city, and lie with her;
24 Then ye shall bring them both out unto the gate of that city, and
ye shall stone them with stones that they die; the damsel, because she
cried not, being in the city; and the man, because he hath humbled his
neighbour's wife: so thou shalt put away evil from among you.
25 But if a man find a betrothed damsel in the field, and the man
force her, and lie with her: then the man only that lay with her shall
die.
26 But unto the damsel thou shalt do nothing; there is in the damsel
no sin worthy of death: for as when a man riseth against his
neighbour, and slayeth him, even so is this matter:
27 For he found her in the field, and the betrothed damsel cried, and
there was none to save her.
Mary might, very likely, have told Joseph about the visitation of Gabriel. If she told, it did not matter for Joseph to worry. He knew that the Law forbids Mary to have had sex before they are married. The thought of putting Mary away is because no one will believe that Mary was pregnant from the Holy Ghost (yet). This makes being pregnant for very dangerous for Mary, and perhaps also for Joseph.