“And He called a famine upon the land; He broke the whole staff of bread. He sent a man before them; Joseph was sold for a servant; his feet they hurt with fetters, his person was laid in iron; until the time that his word came to pass, the word of the L-RD tested him. The king sent and loosed him; even the ruler of the peoples, and set him free.” – Psalm 105:16-20, JPS 1917 Tanach
Joseph’s redemption from prison was procured by divine decree. After interpreting Pharaoh’s dreams, he ascended to second in command of Egypt. For Pharaoh had been so impressed with Joseph’s interpretation, and subsequent advice on how to preserve food in light of the seven year famine that was on the horizon, that he put Joseph in charge. This was the beginning of the fulfillment of Joseph’s own dreams of ascension that indicated his rulership, and the bowing down of his brothers to him.
When Jacob sends his (ten) sons, excepting Benjamin, to Egypt in order to purchase food during the famine, the brothers encounter Joseph, who they do not recognize. He decides to take them prisoners, under the pretense that they are spies, in order that one of them would go back to Canaan, to bring Benjamin to Egypt. This would justify their claim that they were all the sons of one man.
However, he states that he “fears G-d,” so he will only keep one of the brothers in prison, send the rest back to their father Jacob with food, and expect their return. When their food supply runs dry, Jacob sends the brothers back to Egypt with Benjamin in order to procure more food. When they return with Benjamin, they all bow down to the Egyptian prince (Joseph). Thus the first dream is fulfilled.