From How is scripture fulfilled?:
James quoted Genesis 15:6. It is not a prophecy and it is not something that needed to be “fulfilled.” James is not implying that Abraham fulfilled a prophecy when he believed God. What he is doing in this case is quoting the passage in order to conclude his argument and also to give it more weight.
“Fulfill” does not mean that the current point is a prophecy that is now coming true. The usual meaning is that the current point can be rephrased in classical Old Testament language. It is quoted for its rhetorical impact.
Today, instead of saying “fulfilled” we would probably say “We might verbalize the current point in classical Old Testament verbiage” or, “I am reminded of the text” or “This idea gives new meaning to the Old Testament saying.”
We often say contemporary things in phrases that have become classic. When I make an elaborate plan and it fails, I often say, “Oh, the best laid plans of mice and men.” I don’t mean the original poet had my situation in mind. I am recycling his excellent verbiage and applying it to my situation.
So, what was the writer of Matthew doing by applying Hosea 11:1 to Joseph, Mary and Jesus’ trip to Egypt? In simple terms, the Egypt trip reminds the writer of the words he read in Hosea. On a slightly more sophisticated level, he may be thinking of the Egypt trip as a kind of reenactment by Jesus of the Exodus.