Joseph is now a servant in the royal prison, faithfully doing his work and waiting for the day when his prophetic dream would come true. Note Joseph's humility as he interprets the two dreams in chapter 40. He gives all the glory to the Lord!
In chapter 41 Joseph interprets Pharaoh's dream and is exalted to a great standing in Egypt. He was also given a Gentile bride in this chapter. Joseph's marriage can be contrasted to Christ's marriage to the Church. Christ was rejected by His brethren in the flesh, and He came to the Gentile people.
The things that happened to Joseph were but a part of a greater plan, a plan to preserve Israel in the world, and prepare the way for the birth of Christ. That plan was set in motion in chapter 42, when Jacob heard there was grain in Egypt and sent his sons there to secure food.
Ten of the sons went down to Egypt and Joseph recognized them as his brothers, though they did not recognize Joseph. The ten brothers bowed down before Joseph in verse 6, but Joseph's dream had predicted that eleven would bow down before him. This explains how he knew that his brothers would return at a later time, bringing their younger brother, Benjamin, with them.
Joseph was hard on his brothers because he wanted to be sure they had repented of their sin before he forgave them. To forgive a man who is not sincerely repentant is to make him a worse sinner (see Luke 17:3,4). The rough treatment Joseph gave his brothers got results, for they confessed, "We are verily guilty.".
It was because of Joseph's faithfulness that he was used of God. He endured more than 13 years of trials and afflictions, and God rewarded him for his faithfulness. This should be a lesson to each of us to be faithful to God, no matter what may befall us. We have no reason to question God. When we have dedicated our lives to Him and are doing His will, He will faithfully lead us through every situation.