"An Ammontie or a Moabite shall not enter into the assembly of the Lord; even to the tenth generation shall none of them enter into the assembly of the Lord forever; because they met you not with bread and with water in the way when ye came forth out of Egypt; and because they hired against thee Balaam the son of Beor from Pesor of Aram-naharaim to curse thee." (Devarim 23:4-6)
"Thou shalt not abhor an Edomite for he is thy brother; thou shalt not abhor an Egyptian because thou was a stranger in his land. The children of the third generation that are born unto them may enter into the assembly of the Lord." (Devarim 23:8)
There are a number of inconsistencies with these verses that demand explanation. The first is the ban on Ammonites based on lack of hospitality as well as a connection to the incident with Bilaam. As all those who have been paying attention can tell, this makes no sense. For one thing, nowhere in the Torah do we hear that our ancestors came near to the border of Ammon and made a request for sustenance. In addition, it was Midyan that was Moav's partner in the hiring of Bilaam, not Ammon so why are they condemned for it?
Then there's the more obvious problem - we are told to be much nice to the Egyptians - our slavemasters for many decades and who murdered and tortured many of our ancestors - and the Edomites - who not only showed no hospitality but came out in force to prevent a breach of the border - than to the Moabites and Ammonites. Surely their behaviour would have earned them a similar ban?
The Malbim, in his commentary on the parshah, analyzes these questions and presents the following answers:
First of all, he reads the two criticisms of Ammon and Moav separately. Ammon is criticized for not showing hospitality while Moav is criticized for hiring Bilaam. The latter now makes sense but the former still doesn't until he ties in something fascinating.
The ancestor of both nations, Lot, spent much time in Avraham's house and was exposed to much holiness simply by being in his presence but still separated from him and moved to S'dom when relations between the two became strained. However, despite abandoning Avraham and his holy way of life, we know that Lot continued to keep two important behaviours, that of hachnasas orchim and sexual modesty. Proof for both comes from the episode involving destruction of S'dom. Lot testifies at one point that his unmarried daughters are still virgins despite living in such a degenerate society and it is he who takes in the angels as guests when they arrive in the city despite it being against the local law.
This, then, is the base of the reason for the ban on these two nations. Whereas Lot not only allowed guests into his home but imitated Avraham Avinu and actively sought them out (remember he was sitting in the main square looking for some when the angels arrived), his Ammonite descendants failed to live up to that value when our ancestors became their neighbours after destroying Sichon and Og. Similarly, while Lots daughters guarded their chastity and, even though they ultimately wound up sleeping with their father they did it for the highest of reasons - the preservation of the human race - his descendants in Moav sent their daughters to sexually entice our ancestors while they camping in Shittim. This showed that both nations had not only failed to live up to the standards of their progenitor but actively demonstrated opposition to his two good middos.
The Egyptians, on the other hand, did show hospitality to our ancestors. First, they received Yaakov Avinu and co. during the famine. Secondly, even during the worst parts of the slavery they still provided our ancestors with dwellings and food, as we read in Bemidbar. The Edomites, on the other hand, despite acting in a non-brotherly fashion actually demonstrated kinship when they refused to allow our ancestors to pass their borders under threat of war. After all, as Rashi notes, is Edom's blessing not the sword? By demonstrating their willingness to live by Eisav's blessing from Yitzchak Avinu they thereby acknowledged Yisrael's blessing and their connection to Avraham Avinu's family as well. For this they too were rewarded.
Therefore it seems that the reason Egyptians and Edomites can enter the community of Israel is due to their hospitality and acknowledgement of the special blessing of Israel which Moab and Ammon, two nations that actively went against their patrimonial heritage were to be excluded.