Ibn al-Jawzi :rh: says in one of his books:
I think part of the test is when a believer supplicates and receives no response, and he repeats the dua for a long time and sees no sign of a response. He should realize that this is a test and needs patience.
What a person experiences of waswaas (whispers from shaytan) when the response is delayed is a sickness which needs medicine – I have experienced this myself. A calamity befell me and I supplicated and did not see any response, and Iblees started to lay his traps. Sometimes he said: The generosity (of Allah) is abundant and He is not miserly, so why is there a delay?
I said to him: Be gone, O cursed one, for I have no need of anyone to argue my case and I do not want you as a supporter!
Then I told myself: Beware of going along with his whispers, for if there was no other reason for the delay except that Allah is testing you to see whether you will fight the enemy, that is sufficient wisdom.
My soul (nafs) said: How could you explain the delay in the response of Allah to your prayers for relief from this calamity?
I said: It is proven with evidence that Allah, may He be glorified and exalted, is the Sovereign, and the Sovereign may withhold or give, so there is no point in objecting to Him.
The wisdom behind that is proven in definitive evidence. I may think that something is good, but wisdom does not dictate it, but the reason for that may be hidden, just as a doctor may do things that appear outwardly to be harmful, intending some good purpose thereby. Perhaps this is something of that nature.
There may be an interest to be served by delay, and haste may be harmful. The Prophet (peace and blessings of Allah be upon him) said: “A person will be fine so long as he does not become impatient and says, ‘I prayed but I did not receive any answer.’”
The response may be withheld because of some fault in you. Perhaps there was something dubious in what you ate or your heart was heedless at the time when you said the dua, or your punishment is being increased by means of your need being withheld, because of some sin from which you have not repented sincerely. So look for some of these reasons, so that you might achieve your aim.
You should examine the intention behind this request, because attaining it may lead to more sin, or prevent you from doing some good, so withholding it is better.
Perhaps losing what you have missed out on will cause you to turn to Allah and getting it will distract you from Him. This is obvious, based on the fact that were it not for this calamity you would not have turned to Him, because the real calamity is what distracts you from Him, but what makes you stand before Him is good for you and is in your best interests.
If you ponder these things you will focus on what is more beneficial for you, such as correcting a mistake or seeking forgiveness or standing before Allah and beseeching Him, and forget about what you have missed out on.
In the Quran, Allah :swt: says:
“And it may be that you dislike a thing which is good for you” [Surah Al-Baqarah, 2:216]
According to a hadith of the Prophet (SAW):
“There is no Muslim who calls upon his Lord with a dua in which there is no sin or severing of family ties, but Allah will give him one of three things: Either He will answer his prayer quickly, or He will store (the reward for) it in the Hereafter, or He will divert an equivalent evil away from him.” They said: “We will say more dua.” He said: “Allah’s bounty is greater.” [Narrated by Ahmad, 10749; Al-Tirmidhi, 3573. Classed as Saheeh by Al-Albaani in Mishkaat Al-Masaabeeh, 2199]
On a related note, Umm Salamah said: I heard the Messenger of Allah (SAW) say:
“There is no calamity that befalls one of the Muslims and he responds by saying ‘Innaa Lillaahi wa innaa ilahi raaji’oon, Allahumma ujurni fi museebati w’ukhluf li khayran minha (Truly, to Allah we belong and truly, to Him we shall return; O Allah, reward me in this calamity and compensate me with something better than it),’ but Allah will compensate him with something better than it.” [Narrated by Muslim, 918]
Ibn Al-Qayyim said:
Whatever Allah has decreed for His believing slave is a blessing even if that is in the form of withholding; it is a favor even if that is in the form of a trial, and the calamity decreed by him is fair even if it us painful. [Madaarij Al-Saalikeen, 4/215.]
Many ask if Allah is not being merciful with them when not answering their prayers. (The real reason is that) What Allah chooses for His slave is better for him than what he chooses for himself. Allah is more merciful towards His slaves than they themselves or their mothers are. If something happens to them that they dislike, that is better for them than if it did not happen, so His decree is all kindness and mercy. If the slave submits to Allah and has certain faith that all dominion belongs to Allah and all things are under His command, and that He is more merciful to him than he is himself, then he will find peace of mind regardless of whether his need is met or not. [Madaarij Al-Saalikeen, 2/215]
Please read the responses in this thread for detailed explanation:
http://www.islamicboard.com/advice-support/134323562-dua-accepted.html