This is an answer that I heard from one of my Rabbeim. It is from the Sefer Chidushei HaRim, on Bereishis 18:9. While he gives the basic pshat mentioned below, he does not quote every source that I have.
Ultimately the Davidic dynasty comes from Rus, who, in turn, came from Moav, Lot’s son by his eldest daughter. As Moav was only born after Lot was rescued from Sedom, the only reason Lot was able to be saved was because he would go on to be the ancestor of David HaMelech. (In the words of the Midrash1, “‘I have found my servant David.’2 Where did I find him? In Sedom.”)3
But all would be for naught if Moav’s descendant Rus would be unable to marry Boaz. In order for Lot to be able to father Mashiach, Rus needs to be able to marry into Klal Yisrael.
The passuk says,4 “A Moavite and Amonite may not marry into the populace.” In order that Rus be able to marry Boaz, the exposition5 “Moavite and not Moavitess; Amonite and not Amonitess” must be valid. But as Doeg challenged,6 why don’t we make the same exposition of “Egyptian and not Egyptianess”?
To that the Gemara responds7 based on the following verse. Why may a Moavite and Amonite not enter the populace? “Because they did not greet you with bread and water.”8 It’s not tznius for women to go out and greet men with food and water. So why did the women not greet the women? The Gemara answers9 that it’s not tznius for women to be doing that at all. What is its source? Well, one source is from Tehillim 45:14. But the other source it brings is from our very story - “And they said to him, ‘Where is Sarah your wife?’ And he said, ‘She is in the tent.’”10 As Rashi to that verse explains, the Malachim wanted to know why Sarah wasn’t out with Avraham, and Avraham responded that she is a tzanu’ah.
Why do the Malachim need to know this information?
If Rus can’t marry into the congregation, Lot can’t be saved. The Malach that was to destroy Sedom needed Lot to leave before he could do anything.11 Therefore, Gavriel needed to know whether he should wait for Lot to leave before overturning the city. But since he could only be saved on account of Rus, he needed to know the answer to the critical question: where is Sarah your wife? Are women tzenu’os, and therefore the women would not greet the women either, or are they not tzenu’os, and therefore Rus is included in the ban on Moavim? With the answer in hand, the Malachim went to Sedom to save Lot before overturning the city.
1Bereishis Rabbah 41:4
2Tehillim 89:21
3The Gemara in Yevamos 77a says similarly, comparing “found” in this verse to “found” in Bereishis 19:15 about Lot’s daughters. I only picked the Midrash because it makes the point more explicitly.
4Devarim 23:4
5Yevamos 86b
6Ibid.
7Ibid.
8Devarim 23:5
9Yevamos 87a
10Bereishis 18:9
11See Rashi to Bereishis 19:16, that the Malach in charge of destroying Sedom helped take Lot out of the city. The Gur Aryeh’s second understanding of this Rashi is in line with my point above.