Actually, I think it’s being conflated with Mark’s version, which does say: “And the gospel must first be preached unto all the nations.” But then again, the phrase is ambiguous and quite problematic in Mark. In context:
“But be on your guard. For they will deliver you over to councils, and you will be beaten in synagogues, and you will stand before governors and kings for my sake, to bear witness before them. And the gospel must first be proclaimed to all nations. And when they bring you to trial and deliver you over, do not be anxious beforehand what you are to say, but say whatever is given you in that hour, for it is not you who speak, but the Holy Spirit."
Note how the phrase kinda breaks the continuity of thought there. (Matthew has a tidier version.) But in Mark’s version the phrase seems to be connected more clearly with the preaching and eventual persecution of the apostles: it seems that Jesus in this version is referring to them as the ones who are supposed to preach the gospel to “all the nations.” (In Matthew’s gospel at least, Jesus does command them to do so: “make disciples of all nations.”) The way Mark’s version reads, the gospel “must” be preached to “all the nations” first before the apostles are persecuted and “the end” (of Jerusalem, as the context makes clear) comes.
In which case, you can “all the nations” as probably being sort of symbolic: St. Paul already refers to the gospel as being “now disclosed, and … made known to all the nations” (Romans 16:26) and “bearing fruit and growing in the whole world” (Colossians 1:6; cf. Colossians 1:26; Romans 1:5; 8:10; 15:19, 23).
Obviously Paul doesn’t mean that the gospel in his time had already reached literally every corner of the globe, but he could say it was already “made known to all the gentiles” - because he and the others had already propagated it in their world: the Roman Empire and its neighbors. Maybe it’s the same thing here. Jesus was just speaking the language of the people of His time: “all the nations” doesn’t literally have to mean “every country that will exist 2000 or so years from now.”