Allah mentions the favors that He granted Yusuf, peace be on him, by which He made the man from Egypt who bought him, take care of him and provide him with a comfortable life. He also ordered his wife to be kind to Yusuf and had good hopes for his future, because of his firm righteous behavior. He said to his wife,
(Make his stay comfortable, maybe he will profit us or we shall adopt him as a son.) The man who bought Yusuf was the minister of Egypt at the time, and his title was `Aziz'. Abu Ishaq narrated that Abu `Ubaydah said that `Abdullah bin Mas`ud said, "Three had the most insight: the `Aziz of Egypt, who said to his wife,
In the first sentence of the second verse (22), it is said: وَلَمَّا بَلَغَ أَشُدَّهُ آتَيْنَاهُ حُكْمًا وَعِلْمًا (And when he reached at the prime of his age, We gave him wisdom and knowledge ... ).
At what age did he reach the prime of his age? In this, commentators differ. Sayyidna Ibn 'Abbas, Mujahid and Qatadah رحمۃ اللہ علیہم say that his age was thirty three years. Dalhhak jw SII ,~ ~j puts it at twenty, and Hasan al-Basri (رح) at forty. However, all of them agree that the bestowing of wisdom and knowledge upon him referred to at this place means the bestowal of Nubuwwah, the station of prophethood. This also tells us that Sayyidna Yusuf (علیہ السلام) was invested with Nubuwwah much later than his arrival in Egypt - and the Wahy (revelation) sent to him while he was in the depth of the well was not the Wahy (revelation) technically identified with Nubuwwah. Instead of that, it was a revelation in the literal sense which can also be sent to non-prophets - as it has appeared in the case of the mother of Sayyidna Musa (علیہ السلام) ، and about Sayyidah Maryam.
In the second sentence of the second verse (22), it was said: وَكَذَٰلِكَ نَجْزِي الْمُحْسِنِينَ (and this is how We reward those good in deeds). The sense is that delivering Sayyidna Yusuf (علیہ السلام) from elimination and making him reach the office of power and honour was an outcome of his righteousness, fear of Allah and good deeds. This was something not restricted to his person alone. In fact, whoever acts the way he did, shall receive the blessings of Allah in the same fashion.
Tazkirul Quran
Maulana Wahiduddin KhanOn ayahs 22–23
It is said that an officer of the Egyptian government, Potiphar, bought Joseph. He recognized Joseph’s great personality hidden behind an ordinary exterior. He could make out that Joseph was not really a slave but a member of a noble family who, for whatever the reason, had fallen into the hands of the caravan and been sold into slavery. So he asked his wife not to keep Joseph as a slave, but as a family member. He further said that the youth appeared to be a promising young man, capable of managing the affairs of his household and property. Moreover, Potiphar was childless and wanted to adopt somebody as his son. So he intended to adopt Joseph as his son if he lived up to his expectations. When Joseph was about forty years old, God blessed him with prophethood on the one hand and with power and authority on the other. He received this reward owing to his righteous deeds. The door of God’s blessings is always open for righteous people! The only difference is that during the age of prophethood, a deserving person could have been given prophethood as a result of his righteous deeds but, in later periods, (after the Prophet Muhammad, the last messenger of God) he may receive all bounties except that of prophethood.
Uthmani text of the Madinah mushaf (King Fahd Complex). Translation: Saheeh International. Tafsir via Quran.com.