Allah responds to the prayer of distressed, even if he is a kaafir.
al Qurtubi said:
فيجيب المضطر لموضع اضطراره وإخلاصه . وفي الحديث : ثلاث دعوات مستجابات لا شك فيهن دعوة المظلوم ودعوة المسافر ودعوة الوالد على ولده ذكره صاحب الشهاب ; وهو حديث صحيح . وفي صحيح مسلم عن النبي صلى الله عليه وسلم أنه قال لمعاذ لما وجهه إلى أرض اليمن واتق دعوة المظلوم فليس بينها وبين الله حجاب وفي كتاب الشهاب : اتقوا دعوة المظلوم فإنها تحمل على الغمام فيقول الله تبارك وتعالى وعزتي وجلالي لأنصرنك ولو بعد حين وهو صحيح أيضا . وخرج الآجري من حديث أبي ذر عن النبي صلى الله عليه وسلم : فإني لا أردها ولو كانت من فم كافر فيجيب المظلوم لموضع إخلاصه بضرورته بمقتضى كرمه ، وإجابة لإخلاصه وإن كان كافرا ،
Allah responds to the distressed when he shows his desperateness and sincerity.
As it is mentioned in the hadith: “Three supplications are answered without doubt. The supplication of the oppressed, the supplication of the traveler, and the supplication of the parent for his son.” This is mentioned by the author of ash-Shihab which is an authentic hadith.
It is mentioned in Sahih Muslim that Prophet peace be upon him said to Muadh when he sent him to Yemen: “Beware of the supplication of the oppressed, for there is no barrier between it and Allah”.
And it is mentioned in the book ash-Shihab: “Beware of the supplication of the oppressed, for it is raised up to the clouds and Allah says: ‘By my Power, and my Majesty, I will help you even if it is after a while.’” This is also authentic hadith.
And it is mentioned by al Ajury from the hadith of Abu Dharr that the prophet peace be upon him said: I don’t return the supplication (of oppressed) even if it is said by a disbeliever (a Kaafir).
Allah responds to the supplication of the oppressed because he is sincere in his necessity. It happens due to HIS Kindness and HIS acceptance of the sincerity, even if the person is a disbeliever (a Kafir). [Tafsir al Qurtubi under Surah an-Naml verse 62]