Did John know Jesus was the Messiah and Son of God during Jesus's baptism? We see that in John 1:31, John the Baptist said that he did not know Jesus. Some Bible commentators have said that what this meant was that John did not know Jesus was the Messiah.
However, we see in Matthew 3:14, John tried to prevent Jesus from being baptized by him because Jesus needed no repentance. Jesus had no sins to repent. So does that mean that John knew Jesus was the Messiah or not? Did John believe Jesus was perfect or not?
The idea behind John 1:31 when John says I knew Messiah/Jesus not is in the same sense as before in John 1:26.

John answered them, saying, I baptize with water: but there standeth one among you, whom ye know not;

In other words, they knew of the forerunner (John the Baptist), but not the person of whom he was &quot;forerunning&quot;.
Recall Jesus said to His mother about working miracles; not before My time.  We saw Jesus at age 12 asking questions, but nothing is recorded about Him being Messiah until His baptism when the Spirit descended, clarifying He was of the Melchizedek priesthood.

And I [John] knew him [Messiah] not: but he [God] 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.

Regarding Matthew 3:14, it appears to be a reference to when Mary came to visit Elizabeth as recorded in Luke 1.

And whence is this to me, that the mother of my Lord should come to me? For, lo, as soon as the voice of thy salutation sounded in mine ears, the babe leaped in my womb for joy.
Luke 1:43-44

When Jesus is about age 30 and John is about 30 1/2, Jesus again comes to John and it is apparent that John &quot;knew&quot; intuitively that Jesus was Messiah at that point.  John baptizes Jesus, the Spirit descends, and confirmation of what John had been foretold did happen.
So, from the bookends of the womb to pre-baptism, John would not in-between know Jesus was Messiah.  At His baptism, John would fully know, though doubt might enter later.
At the same time, John would know the role, function, detail of Messiah of whom he was &quot;clearing the way&quot;.  Messiah would be perfect.