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 "forerunning".
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.
When Jesus is about age 30 and John is about 30 1/2, Jesus again comes to John and it is apparent that John "knew" 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 "clearing the way". Messiah would be perfect.