Virtually all translations use a simple past tense in John 1:31 (and likewise in 1:33):
KJV: I knew him not ...
ESV, NIV: I myself did not know him ...
NASB: I did not recognize him ...
but the tense used in the Greek is the pluperfect, οὐκ ᾔδειν αὐτόν; perhaps better translated "I hadn't known Him."
This means that the premise that John did not know Jesus until after Jesus' baptism is not necessarily true. The Greek text says that he did not know Him up to some point in the past, after which He came to know Him; but the text does not say when that point was. Matthew's account makes clear that whenever that moment was, it must have occurred before His baptism.
John Chrysostom (c 349-407) held that the reason John stated twice that he hadn't know Jesus previously was to emphasize to his listeners that the attention that he (John the Baptist) called to Jesus was not merely due to the fact that Jesus was his relative:
Here he renders his testimony free from suspicion, by showing that it was not from human friendship, but had been caused by divine revelation. “I knew Him not,” he saith. How then couldest thou be a trustworthy witness? How shalt thou teach others, while thou thyself art ignorant? He did not say “I know Him not,” but, “I knew Him not”; so that in this way he would be shown most trustworthy; for why should he have shown favor to one of whom he was ignorant?1
This appears to have been the patristic consensus, as Theophylact made the same point in his synopsis some 600 years later.2
This is not directly related to your question, but Chrysostom here also makes an interesting observation regarding the Apocryphal childhood narratives of Jesus, which describe things that Jesus did that are not in the canonical Gospels:
He puts the “I knew Him not” repeatedly. On what account, and wherefore? He was His kinsman according to the flesh. “Behold,” saith the angel, “thy cousin Elisabeth, she also hath received a son.” (Luke 1:36) That therefore he might not seem to favor Him because of the relationship, he repeats the “I knew Him not.” And this happened with good reason; for he had passed all his time in the wilderness away from his father’s house.
How then, if he knew Him not before the descent of the Spirit, and if he then for the first time recognized Him, did he forbid Him before baptism, saying, “I have need to be baptized of Thee, and comest Thou to me?” (Matthew 3:14), since this was a proof that he knew Him very well. Yet he knew Him not before or for a long time, and with good cause; for the marvels which took place when He was a child, as the circumstances of the Magi and others the like, had happened long before, while John himself was very young, and since much time had elapsed in the interval, He was naturally unknown to all. For had He been known, John would not have said, “That He should be made manifest to Israel, therefore am I come baptizing.”
Hence it remains clear to us, that the miracles which they say belong to Christ’s childhood, are false, and the inventions of certain who bring them into notice. For if He had begun from His early age to work wonders, neither could John have been ignorant of Him, nor would the multitude have needed a teacher to make Him known.3
1. Homily XVII on John (tr. from Greek)
2. Explanation of the Holy Gospel According to St. John
(tr. from Greek, Chrysostom Press, 2007), p.31
3. Op. cit.