"And the Lord remembered Sarah" (Genesis 21:1). What is written above on this matter? "And Abraham prayed" etc. (Genesis 20:17). A parable. To what is the matter comparable? To a king who had a friend whom he loved exceedingly, and whatever his friend would ask of him he would do. Since everyone knew that the king loved him exceedingly, anyone who sought an office would come to him, and he would ask the king, and he would do his desire for him. Yet to that friend he gave no office at all. The members of his palace said to him: "Our lord the king, this friend of yours asks for others, but for himself he asks nothing." Who is this friend? This is Abraham, as it is said: "The seed of Abraham My friend" (Isaiah 41:8). And when Abimelech took Sarah, the Holy One, blessed be He, stopped up all their fountains, as it is said: "For the Lord had completely closed up" (Genesis 20:18). But when Abimelech asked of Abraham, he was healed, for he prayed over him, as it is said: "And Abraham prayed to God" (ibid., ibid. 17). The ministering angels said before the Holy One, blessed be He: "Master of the universe, Abraham heals others, yet he himself needs healing. He healed Abimelech and his household, and they bore children, as it is said, 'And God healed' — and You do not heal him?" The Holy One, blessed be He, said: "He is worthy that I should give him children." See the deed of the Holy One, blessed be He, that it is not like the deed of flesh and blood. A man says he will give his fellow a gift; sometimes he gives, sometimes he does not give. But the Holy One, blessed be He, is not so: when He says He will do good, immediately He does good. Balaam said: "God is not a man, that He should lie" (Numbers 23:19), and then he said: "Hath He said, and shall He not do it?" (ibid.). Rabbi Shamshu'el bar Nachman said: This verse — its end does not correspond to its beginning, nor its beginning to its end, for he says "God is not a man, that He should lie" etc., and then he says "Hath He said, and shall He not do it?" Rather: it is a son of man who "has said, and shall not do it," but the Holy One, blessed be He, says and does, decrees and fulfills, as it is said: "And the Lord remembered Sarah" (Genesis 21:1).
Why God Gave Abraham a Son After He Prayed for Abimelech
Midrash Tanchuma Buber, Vayera 36:1
וה' פקד את שרה. מה כתיב למעלה מן הענין, ויתפלל אברהם וגו' (בראשית כ יז), משל למה הדבר דומה, למלך שהיה לו אוהב והיה מחבבו ביותר, וכל מה שהיה אוהבו שואל ממנו היה עושה, כיון שהיו הכל יודעים שהמלך מחבבו ביותר, וכל מי שהיה מבקש שררה היה בא אצלו והוא מבקש מן המלך ועשה לו חפצו, ולאותו אוהב לא נתן שררה מאומה, אמרו לו בני פלטין שלו אדונינו המלך אוהבך זה מבקש לאחרים, ולנפשו אינו מבקש, מי הוא זה האוהב, זה אברהם, שנאמר זרע אברהם אוהבי (ישעיה מא ח). וכיון שנטל אבימלך את שרה, סתם הקב"ה כל מעיינותיהם, שנאמר כי עצור עצר ה' (בראשית כ יח), אלא כיון שבקש אבימלך מאברהם נתרפא, כי התפלל עליו, שנאמר ויתפלל אברהם אל האלהים (שם שם יז), אמרו מלאכי השרת לפני הקב"ה רבונו של עולם אברהם מרפא אחרים, והוא צריך רפואה, ריפא לאבימלך ולביתו וילדו, שנאמר וירפא אלהים, ואין אתה מרפא אותו, אמר קב"ה כדאי הוא שאתן לו בנים. ראה מעשה של הקב"ה שאינו כמעשה בשר ודם, אומר אדם ליתן לחבירו מתנה, פעמים נותן, פעמים אינו נותן, אבל הקב"ה אינו כן, כשהוא אומר להיטיב, מיד הוא מטיב, אמר בלעם לא איש אל ויכזב (במדבר כג יט), וחזר ואמר, ההוא אמר ולא יעשה (שם), אמר ר' שמשואל בר נחמן הפסוק הזה לא סופו ראשו, ולא ראשו סופו, שהוא אומר לא איש אל ויכזב וגו', וחזר ואמר ההוא אמר ולא יעשה, אלא בן אדם, ההוא אמר ולא יעשה, אבל הקב"ה אומר ועושה, גוזר ומקיים, שנאמר וה' פקד את שרה.