John's question in prison John the Baptist was someone whose job was to prepare the way from Christ. When Jesus makes his appearance, John introduces him to the people.

John 1:29-34, New Living Translation (NLT)

Jesus, the Lamb of God


  The next day John saw Jesus coming toward him and said, “Look! The Lamb of God who takes away the sin of the world! 
  He is the one I was talking about when I said, ‘A man is coming after me who is far greater than I am, for he existed long before me.’ 
  I did not recognize him as the Messiah, but I have been baptizing with water so that he might be revealed to Israel.”
  Then John testified, “I saw the Holy Spirit descending like a dove from heaven and resting upon him. 
  I didn't know he was the one, but when God sent me to baptize with water, he told me, ‘The one on whom you see the Spirit descend and rest is the one who will baptize with the Holy Spirit.’ 
  I saw this happen to Jesus, so I testify that he is the Chosen One of God.”


John 3:31-36, New Living Translation (NLT)


  “He has come from above and is greater than anyone else. We are of the earth, and we speak of earthly things, but he has come from heaven and is greater than anyone else. 
  He testifies about what he has seen and heard, but how few believe what he tells them! 
  Anyone who accepts his testimony can affirm that God is true. 
  For he is sent by God. He speaks God’s words, for God gives him the Spirit without limit. 
  The Father loves his Son and has put everything into his hands. 
  And anyone who believes in God’s Son has eternal life. Anyone who doesn't obey the Son will never experience eternal life but remains under God’s angry judgment.”


Mark 1:9-14, New Living Translation (NLT)

The Baptism and Temptation of Jesus


  One day Jesus came from Nazareth in Galilee, and John baptized him in the Jordan River. 
  As Jesus came up out of the water, he saw the heavens splitting apart and the Holy Spirit descending on him like a dove. 
  And a voice from heaven said, “You are my dearly loved Son, and you bring me great joy.”
  The Spirit then compelled Jesus to go into the wilderness, 
  where he was tempted by Satan for forty days. He was out among the wild animals, and angels took care of him.
  Later on, after John was arrested, Jesus went into Galilee, where he preached God’s Good News.


So the basic timeline is: Jesus enters the scene. --> John proclaims that he is the Messiah. --> John Baptizes. --> John is put in prison.

So, how should I understand this:

Matthew 11:2-3, New Living Translation (NLT)


  John the Baptist, who was in prison, heard about all the things the Messiah was doing. So he sent his disciples to ask Jesus, 
  “Are you the Messiah we've been expecting,[a] or should we keep looking for someone else?”


Well, this is confusing, since that way John has been talking it is implying that he knew who was Messiah or not. As far as I know he was executed in prison, so the timeline cannot be backwards.

Answers?
The notes in my Bible say:


  [11:3] The question probably expresses a doubt of the Baptist that Jesus is the one who is to come (cf. Mal 3:1) because his mission has not been one of fiery judgment as John had expected (Mt 3:2).


So the timeline becomes:


  Jesus enters the scene. --> John proclaims that he is the Messiah. --> John Baptizes. --> John is put in prison. --> John expresses doubt because he hears things are not going as he expected them to.


This might have been because John was in some way ill-informed of exactly what was happening, because Jesus sends the following reply:


  Matthew 11:4-6
  4 Jesus said to them in reply, “Go and tell John what you hear and see:
  5 the blind regain their sight, the lame walk, lepers are cleansed, the deaf hear, the dead are raised, and the poor have the good news proclaimed to them.
  6 And blessed is the one who takes no offense at me.”


Many of the people closest to Jesus express serious doubts at one point or another -- Thomas in particular comes to mind. (It should be noted, though, that many others, like His Blessed Mother, Mary, never doubt.)