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.
Related Quran Ayaaths of this Hadith.
Surah 6 : Ayah 84 Surah 12 : Ayah 96 Surah 12 : Ayah 99 Surah 12 : Ayah 100 Surah 12 : Ayah 102 Surah 12 : Ayah 111"When a person dies, his works end, except for three: ongoing charity, knowledge that is benefited from, and a righteous child who prays for him."
Prophet Mohammed (PBUH)
"The best of what a man leaves behind are three: a righteous child who supplicates for him, ongoing charity the reward of which reaches him, and knowledge that is acted upon after him."
Sunan Ibn Mājah
"Every day two angels come down from Heaven and one of them says, 'O Allah! Compensate every person who spends in Your Cause,' and the other (angel) says, 'O Allah! Destroy every miser.'"
Sahih Bukhari