The two years refer to the additional two years Yosef remained in prison. Initially, Hashem decreed that Yosef would be imprisoned for ten years for “slandering” his ten brothers. Hashem, however, extended his incarceration, because he placed his trust in the wine steward. In addressing Yosef’s misplaced trust, the Midrash seems to be inconsistent. They cite a pasuk in Tehillim 40:5 which states, “Praiseworthy is the man who has made Hashem his trust.” Chazal interpret this to be a reference to Yosef, whose faith in the Almighty was unshakeable. Chazal interpret the second part of the pasuk, “And turned not the arrogant,” as a criticism of Yosef for twice asking the wine steward to remember him before Pharaoh. How are we to reconcile the Midrash which seems to laud and castigate Yosef simultaneously?
The Kotzker Rebbe, zl, comments that one has attained an impressive level of piety if Hashem punishes him immediately for any infraction. Indeed, Hashem is clearly indicating to him the area in which he erred. The righteous person immediately corrects his ways, seeking penance for his indiscretion. Because of his superior level of bitachon, trust, in the Almighty, Yosef attained that position in Hashem’s eyes as soon as he misplaced his trust, regardless of the trivial nature of his act, Hashem immediately punished him. This is the pasuk’s message: Yosef’s trust in Hashem was so profound that any laxity resulted in immediate disciplinary action.