Just as John the Baptist knew in the womb that Mary was carrying the Mesiah and had stated that one greater than he would follow him, he knew that Christ was the Lamb of God. John was given that knowledge by the Holy Spirit.
John knew that sin in the world needed to be taken away and the Jesus was the sacrafice sent to do that for mankind.