So why was Yosef punished?
The first possibility is because he was destined to be the ruler of Egypt.
A leader is always held to a higher standard. Even though some immoral actions are overlooked when done by ordinary people, when they’re done by presidents or prime ministers they become unacceptable scandals. Everyone understands that the leader is supposed to be a role model.
Yosef was to be an example for the entire Egypt and beyond, so it was important for him to have impeccable middos.
The other answer is that Yosef was previously on a high level of bitachon. In fact, he knew his brothers may want to harm him and still he went to check on them for his father.
Now that Yosef slipped and started doing hishtadlus to get out of prison, it was worthwhile for him to remain in prison for an extra two years in order for Yosef to get back to his original madregah. Hashem often helps people get back to spiritual heights they have lost.
What’s more, these lessons were so important for Yosef, that the entire history of the world was postponed for two years! Yosef being freed put into motion the entire story of the slavery in Egypt and ultimately the redemption which lead to matan Torah. All this was pushed off for just one person to reach greater heights in bitachon.