Between Vayeshev and Miketz
At the end of Parashat Vayeshev, Yosef asks the Sar Hamashkim to help with his release, because he was unjustly imprisoned (Bereshit 40:14-15).
In the beginning of Parashat Miketz, two years have passed. Only after Pharaoh’s dream, the Sar Hamashkim remembers about Yosef (Bereshit 41:9-13).
This is actually somewhat strange and feels asymmetric.
Yosef asks to be released because of justice. However, that’s not why he’s released. He’s released because of his ability to interpret dreams – his wisdom.
Why wasn’t he released for the same reason he asked for?
I think this has to do with a major problem with Egypt. Justice isn’t a serious concern there. Wisdom, however, is highly valued.
Even in Avraham’s time, Sarah was taken unjustly from him. On the other hand, the obsession with wisdom is clear from the frequent discussion of chachmei Paro.
This can explain the two years between Yosef’s appeal and his release. If it was about justice – he would have been released right away. Justice delayed is justice denied. But that had to wait until such time that Yosef’s wisdom would be appreciated.
Yosef became caught up in the world of wisdom. He ignored the brother’s requests for justice – they also were imprisoned despite having actually done no wrong (certainly not violating any Egyptian laws). And Yosef responded by asking how dare they challenge his wisdom, expressed in his ability to divine the future (see Rashi on Bereshit 44:15).
In the end, Yosef did acknowledge the importance of justice, and told Yehuda that only he who was guilty would be punished. But he did not succeed in changing Egypt’s priorities. His wisdom continued to be appreciated, bringing Egypt prosperity, but at the cost of unjustly enslaving the populace.
This continued in the beginning of Shemot. When dealing with Bnei Yisrael, the Egyptians used wisdom (“Let us act wisely” – Shemot 2:10) to unjustly place them in slavery and brutally oppress them. The lesson would only be taught through the plagues, where chachmei paro couldn’t use their wisdom, and the ultimate punishments Egypt received for ignoring justice.