You’ve raised a very good question. I think the problem is more a result of the translation than of the text itself. The King James Version renders, “And I knew him not. . .” which suggests that John did not know Jesus personally, and perhaps that he did not even know of Him. This seems somewhat unlikely. But once John 1:33 is rendered, “I did not recognize Him (as the Messiah). . .” the problem is largely solved.
You remember that some of those who came to be baptized by John did so hypocritically (Matthew 3:7-12). John told them to produce fruit that was worthy of repentance (3:8).
John tells us that when Jesus came to him, he did not yet know for certain that Jesus was the Messiah. The confirmation of his identity came at His baptism, when the Spirit abode on the Lord Jesus. So John is not saying that he did not know Jesus, or of His life, but that he had not yet received positive divine confirmation as to the identity of the Messiah. (I think John must have wondered. The Jews were even asking John if he were the Messiah - Luke 3:15)
When Matthew describes the arrival of Jesus at the place where John was baptizing, John obviously did know of Jesus (something a correct translation of John 1:33 does not contradict). While John had not yet received the revelation that Jesus was the Messiah, he must have known that Jesus (in contrast to the hypocritical Pharisees and Sadducees) was a truly righteous man. John, humble man that he was, felt as though Jesus were his superior, and because of this he protested that he should not be the one baptizing, but the one to be baptized. Jesus convinced John to proceed with the baptism, and it was in the course of our Lord’s baptism that divine confirmation of Jesus’ identity as the Messiah was given.
As I read Matthew 3, John never protests on the basis that Jesus is surely the Messiah, but rather on the basis that Jesus is more righteous than he, his spiritual superior.
From this point of view the two texts in Matthew and John compliment each other, rather than contradict each other.
KJV John 1:33 And I knew him not: but he that sent me to baptize with water, the same said unto me, Upon whom thou shalt see the Spirit descending, and remaining on him, the same is he which baptizeth with the Holy Ghost.
NIV John 1:33 I would not have known him, except that the one who sent me to baptize with water told me, ‘The man on whom you see the Spirit come down and remain is he who will baptize with the Holy Spirit.’
NAS John 1:33 “And I did not recognize Him, but He who sent me to baptize in water said to me, ‘He upon whom you see the Spirit descending and remaining upon Him, this is the one who baptizes in the Holy Spirit.’
NLT John 1:33 I didn’t know he was the one, but when God sent me to baptize with water, he told me, ‘When you see the Holy Spirit descending and resting upon someone, he is the one you are looking for. He is the one who baptizes with the Holy Spirit.’