The context of this statement shows it to be the words of Joseph (peace be upon him).
When Allah willed for Joseph's release from prison, He influenced the dream of the king.
The king (of Egypt) said: "I do see (in a vision) seven fat cows, whom seven lean ones devour, and seven green ears of corn, and seven (others) withered. O you chiefs! Expound to me my vision if it be that you can interpret visions." [Sûrah Yûsuf: 43]
None of the chiefs could do so. No one was able to interpret the dream besides Joseph, who said:
"For seven years shall you diligently sow as is your wont: and the harvests that you reap, you shall leave them in the ear,- except a little, of which you shall eat. Then will come after that (period) seven dreadful (years), which will devour what you shall have laid by in advance for them,- (all) except a little which you shall have (specially) guarded. Then will come after that (period) a year in which the people will have abundant water, and in which they will work their presses." [Sûrah Yûsuf: 47-49]
Because of his success in relating the dream's significance, the king was impressed and summoned him. However, Joseph refused to come out of the prison until an official investigation was opened regarding his case.
"So the king said: 'Bring him unto me.' But when the messenger came to him, (Joseph) said: 'Go back to your lord, and ask him, 'What is the state of mind of the ladies who cut their hands'? For my Lord is certainly well aware of their snare." [Sûrah Yûsuf: 50]
During the ensuing investigation, the women gave their testimony as to what really took place and the king's wife confessed that Joseph had been innocent all along.
"(The king) said (to the ladies): 'What was your affair when you did seek to seduce Joseph from his (true) self?' The ladies said: 'Allah preserve us! No evil know we against him!' The ruler's wife said: 'Now the truth is manifest (to all): it was I who sought to seduce him from his (true) self: He is indeed of those who are (ever) true (and virtuous)'." [Sûrah Yûsuf: 51]
Joseph's refusal to leave the prison after all the long years he had spent as a prisoner is an extraordinary thing. Prophet Muhammad (peace be upon him) was moved to say: "May Allah bless my brother Joseph. If that messenger had come to me after that long imprisonment, I would have left in compliance immediately."
Therefore, it makes sense that Joseph (peace be upon him) would explain why he refused to leave prison until after his innocence and integrity were established, saying:
"This is so he might know that I had not been unfaithful to him in secret and that Allah does not guide the device of the unfaithful." [Sûrah Yûsûf: 52]
Ibn `Abbâs says: "Had Joseph left at that time before the king knew the truth about his affairs, Egypt's ruler would have always harbored bad thoughts in his heart that this was the man who made a move on his wife."
Right after justifying his noble intentions for refusing to leave prison when he was summoned, Joseph (peace be upon him) says, showing his humility:
"Nor do I absolve my own self (of blame): the (human) soul is certainly prone to evil, unless my Lord bestows His mercy: but surely my Lord is Oft-forgiving, Most Merciful." " [Sûrah Yûsuf: 53]
Joseph's saying this after his innocence had been established with certainty endeared him to the king all the more. The king did not doubt Joseph's innocence in the least. He, instead, wanted to keep Joseph with him, and in this way Allah caused Joseph to be established with authority in the land.
"So the king said: 'Bring him to me; I will take him specially to serve about my own person.' Therefore when he had spoken to him, he said: 'Be assured this day, you are, before our own presence, with rank firmly established, and fidelity fully proved!'
"(Joseph) said: 'Set me over the store-houses of the land: I will indeed guard them, as one that knows (their importance).'
"Thus did We established Joseph with power in the land, to take possession therein as, when, or where he pleased. We bestow of our Mercy on whom We please, and We do not let the reward of those who do good be lost." [Sûrah Yûsuf: 54-57]