Jesus fasted and prayed for 40 days and 40 nights to prepare for the ministry God assigned to him. In the New Testament, the gospels of Luke, Mark and Matthew have passages that refer to Jesus' stay in the wilderness. His time in the desert occurs after his baptism by John the Baptist.
In these gospels, there are passages that state that the Holy Spirit drove Jesus into the desert wilderness. It was a place of testing where Jesus was tempted three times by the Devil. Although Jesus was tempted, he remained faithful to God's will for his life.
In the Bible, the number 40 is associated with the testing of the Israelites as they wandered through the desert. Moses and Elijah were other Biblical figures that went into the desert for periods of fasting, prayer and testing.