In Matthew's gospel, when Jesus comes to John to be baptised, John hesitates, as he feels unworthy. This is Matthew 3:13-16:
So he knows who Jesus is before anything happens. However, this is John 1:32-34:
The most natural reading of John is that the Baptist did not recognise Jesus as the Messiah until after he witnessed the Spirit descending on him (which in Matthew, happens after the baptism). However, it is possible to say that "I did not know him" means "I was not acquanted with him". In that case, John's words mean something like:
This just about works, and doesn't contradict Matthew, though it's not really a natural reading. Also, since I don't read ancient Greek, I don't know if it's actually a legitimate way to understand the text.
A less plausible suggestion that I've seen is that "I did not know him" means "I was not completely sure it was him", but this doesn't work in English and I wonder if it works in the Greek.
A third suggestion is that Jesus basically told John who he was, and John, not knowing whether to believe him, played along (as described in Matthew) and only became sure after the baptism. This view is problematic, though: in Luke 1:41 John seems to have the ability to just sense the presence of holiness.
At any rate, there's a lot of wriggle room here.