He said: "0 my Lord! Prison is more to my liking than that to which they invite me. Unless You turn away their plot from me, I will feel inclined towards them and be one (of those who commit sin and deserve blame or those who do deeds) of the ignorants."
So his Lord answered his invocation and turned away from him their plot. Verily, He is the All-Hearer, the AllKnower. Surah 12: 33-34
That evening, Zulaikha convinced her husband that the only way to save her honor was to put Joseph in prison; otherwise she would not be able to control herself or to safeguard his prestige. The chief minister knew Joseph was absolutely innocent, that he was a young man of honor, a loyal servant, and he loved him for these reasons. It was not an easy decision for him to put an innocent man behind bars. However, he was left with no choice. He reasoned that Joseph's honor would also be safeguarded if he was kept out of Zulaikha's sight. That night, with a heavy heart, the chief minister sent Joseph to prison.
Joseph's Time in Prison
Prison was Joseph's third test. During this period Allah blessed him with an extraordinary gift: the ability to interpret dreams. At about the same time two other men landed in the prison. One was the cupbearer of the king; the other was the king's cook. The two men sensed that Joseph was not a common criminal, for an aura of piety glowed on his face. Both men had vivid dreams, and they were anxious to have them explained. The king's cook dreamed that he stood in a place with bread on his head, and two birds were eating the bread. The cupbearer dreamed that he was serving the king wine. The two went to Joseph and told him their dreams, asking him to give them their meaning.
First, Joseph called them to Allah. Then he said that the cook would be crucified until he died and that the cupbearer would return to the service of the king. Joseph told the cupbearer to remember him to the king and to say that there was a wronged soul called Joseph in the prison. What Joseph predicted did happen; the cook was crucified and the cupbearer returned to the palace.
After the cupbearer returned to service, Satan made him forget to mention Joseph's name to the king. Therefore, Joseph remained in prison for a few years, but he made patience his own, praying to Allah.
Joseph's Time in Prison - Qur'anic
Almighty Allah narrated: And there entered with him two young men in the prison. One of them said: "Verily, I saw myself (in a dream) pressing wine." The other said: "Verily, I saw myself (in a dream) carrying bread on my head and birds were eating thereof" (They said): "Inform us of the interpretation of this. Verily, we think you are one of the Muhsineen (doers of good)."
He said: "No food will come to you (in wakefulness or in dream) as your provision, but I will inform (in wakefulness) its interpretation before it (the food) comes. This is of that which my Lord has taught me. Verily, I have abandoned the religion of a people that believe not in Allah and are disbelievers in the Hereafter. And I have followed the religion of my fathers, - Abraham, Isaac and Jacob and never could we attribute any partners whatsoever to Allah. This is from the Grace of Allah to us and to mankind, but most men thank not (i.e. they neither believe in Allah, nor worship Him).
"0 two companions of the prison! Are many different lords (gods) better or Allah, the One, the Irresistible? You do not worship besides Him but only names which you have named (forged), you and your fathers, for which Allah has sent down no authority. The command (or the judgment) is for none but Allah. He has commanded (i.e. His Monotheism), that is the (true) straight religion, but most men know not.
"0 two companions of the prison! As for one of you, he (as a servant) will pour out wine for his lord (King or master) to drink; and as for the other, he will be crucified and birds will eat from his head. Thus is the case judged concerning which you both did inquire."
And he said to the one whom he knew to be saved: 'Mention me to your lord (i.e. your King, so as to get me out of the prison)." But Satan made him forget to mention it to his lord or Satan made (Joseph) to forget the remembrance of his Lord (Allah) as to ask for His Help, instead of others. So (Joseph) stayed in prison a few (more) years. Surah 12: 36-42