After being ordered out of Avraham’s house, Hagar and her son Yishmael stumbled through the wilderness. Yishmael became feverish and drank all the water available to quench his burning thirst. He could no longer walk by himself and his mother could not hold him any longer. She put him down under a bush, and moved away to pray for his life saying, “I can no longer watch the child dying.” Yishmael also prayed and Hashem responded to his prayer, not Hagar’s. This is because Yishmael had the merit of being Avraham’s son, and also because he performed teshuva (repentance) at that moment.
Horav S.R. Hirsch Z”l notes that Hagar, an Egyptian woman, does not act as a Jewish mother would have acted under similar circumstances. A Jewish mother would not have abandoned her dying child saying she could no longer watch her child suffer. This is the mark of a selfish person, one who is more concerned with her own emotional stability than her child’s well-being. A Jewish mother would have sat at her child’s side, holding his hand, consoling and comforting him until the very end. Yet, Hagar was too anxious to remain with Yishmael. Overwhelmed with her own self-pity, she prayed to Hashem on behalf of her son. Hashem responded to Yishmael’s prayers and not Hagar’s, because she had been thinking of herself rather than her child.
Hashem responded to Yishmael – oa tuv ratc (in the condition he is now). We may derive from here that Hashem is eager to judge a person in his newly purified form, disregarding past offenses. If one repents and puts his best forward, then Hashem views him positively, consistent with his current behavior. Thus, from this narrative we learn that if a person who is overwhelmed with anguish cries out to Hashem, Hashem will heed his prayer.