The way each writer began his description is slightly different, but not really contradictory. It tells us he probably raised the question as they were arriving in the area. Luke tells us the Lord had been praying. His question comes on the heels of his prayer and probably harks back to the large crowds they had recently been addressing.
Ever impulsive, ever a man of action, Simon Peter was quick to give an answer to the question.
The next verse is also often sited as Jesus giving the keys to Peter. I admit, this verse is a bit ambivant, but I believe we must take it in context to the whole passage. Who was Jesus addressing in this passage from beginning to end? The Disciples. What is His purpose? To establish their faith in their own minds before he tells them what is to come, something that they are not expecting, that being that Jesus is going to be killed.
And from this point he begins his instruction to them of how he will suffer and will rise from the dead. Again, Peter, the impulsive, take action on his own and is immediately put in his place.
FROM THE NEW TESTAMENT
When Jesus tells the parable of the two men who built their homes, He makes clear what foundation is important. It is that built upon the Rock of Jesus Christ, Savior and God.