The temptations of Jesus are recorded in detail in Matthew 4 and Luke 4. The Bible tells that what took place was not by chance, but by the providential will of God: Matthew 4:1: "Then was Jesus led up of the Spirit into the wilderness to be tempted of the devil."
The book of Hebrews tells the reason why this took place. As God, Jesus did not know temptation or sufferings, but as a man He experienced the same temptations and sufferings as we do -- and yet He did not sin.
Hebrews 2:17-18: "Therefore, in all things He had to be made like His brethren, that He might be a merciful and faithful High Priest in things pertaining to God, to make propitiation for the sins of the people. For in that He Himself has suffered, being tempted, He is able to aid those who are tempted."
Hebrews 4:15: "For we do not have a High Priest who cannot sympathize with our weaknesses, but was in all points tempted as we are, yet without sin."
Tempted by Satan
In a weakened state, after having fasted for forty days and forty nights, Jesus was taken into the wilderness by the Spirit. Satan came to Him with the intention of tempting Him to sin. In this passage, for the third time in Scripture, we read of Satan speaking directly to someone (cf Genesis 3, Job 1).