“Whether a string or a shoe strap, I will [not] take anything that is yours” (B’reishis 14:23). Avra[ha]m refused to keep any of the property he took possession of when he defeated the four kings (who had taken the possessions of the people of Sodom and its neighboring cities), even though “to the victor belong the spoils.” However, when Avraham went down to Egypt, he had asked his wife to tell the Egyptians that she was his sister “so that they will be good to me on your behalf” (12:13), which Rashi tells us means that they will “give me gifts.” Why did Avraham accept the items that Pharaoh gave him (see Rashi on 12:16), yet be unwilling to keep the items he legally owned, returning them to their original owner (Sodom) instead?
One obvious difference is that Pharaoh gave him these gifts willingly (even if under false pretenses), and refusing to accept them likely would have caused animosity (defeating the purpose of not revealing that Sara [Sarai] was his wife), whereas the King of Sodom considered the possessions to still be his (or wrongly taken from him, even if it wasn’t Avraham who took them), so his offer to let Avraham keep them was made reluctantly (see Or Hachayim on 14:23). Although this only addresses Avraham’s willingness to accept Pharaoh’s gifts while refusing to keep the possessions that originally belonged to Sodom, not why he seemed to have wanted the gifts in the first place, there are reasons why Avraham would want the Egyptians to be good to him (if nothing else, some who might have tried to harm him would refrain from doing so after seeing how well others treated him, see Gur Aryeh on 12:13). Therefore, since Pharaoh really wanted Avraham to keep his gifts (especially after seeing how G-d miraculously protected his wife, see Rashi on 12:17), while the King of Sodom did not, Avraham refused to keep the possessions that had originally belonged to Sodom even though he seemed to have no issue with keeping Pharaoh’s gifts. Nevertheless, a plethora of suggestions have been made to explain why Avraham was willing to accept Pharaoh’s gifts (and even wanted them) but gave Sodom (et al) their possessions back.
Sifsay Chachamim (12:13) offers three answers. First he differentiates between small gifts and large gifts, with the small gifts being room and board, which was fine for Avraham, a guest in a foreign country (Egypt), to accept, and large gifts, which is what the possessions of Sodom would have constituted (as evidenced by Avraham saying that he didn’t want the King of Sodom to say that he made him rich). However, we are told that Avraham had already become rich in Egypt (12:16, 13:2/5), even though he hadn’t had enough to pay his bills on his way to Egypt (see Rashi on 13:3), so unless Avraham became a better businessman in Egypt than he had been in Canaan (12:16 strongly indicates that he became rich because of the gifts given to him, see Gur Aryeh on 12:13, but Maharil Diskin explains it as being able to operate under more business-friendly conditions than others), or G-d decided to start fulfilling His blessing to Avraham in Egypt (see Rashi on 12:2, but see Maskil L’Dovid below), it would seem that the gifts he received in Egypt could not be considered “small,” and were more than just room and board.
His second answer might offend some, so I won’t repeat it here. The only comment I’ll make is that it would be difficult to differentiate between what was said before they were in Egypt and what actually happened, as Avraham did allow them to give him gifts (see 12:16). His third answer is that Avraham only accepted gifts in Egypt because he was poor and needed to pay his bills (referencing Rashi on 13:3), whereas he was already rich when he refused to accept the King of Sodom’s offer. Although Avraham saying he didn’t want the King of Sodom to say that he was the one who made him rich might indicate that he must not have been rich yet, we don’t know if Avraham’s stated concern was real or was only intended for the King of Sodom (see Daas Sofrim), if Avraham was concerned that the King of Sodom might say it even if it wasn’t true, or if the years between Avraham and Lot separating and the war with the kings had impacted either Avraham’s wealth or his perception of what’s considered wealthy. In any case, Avraham certainly had more when he refused to keep the spoils of war than when he accepted Pharaoh’s gifts.
Maharal (Gur Aryeh) offers a couple of suggestions as well. First (on 12:13) he differentiates between the King of Sodom compensating Avraham for saving him, which would not constitute Avraham becoming rich through G-d’s blessing (see Rashi on 12:2 and 14:23), and Pharaoh giving him gifts in order to honor him, which would. Then (also on 12:13) he says Avraham might have refused to accept Pharaoh’s gifts too if not for the circumstances, namely needing to gain the respect of the Egyptian populace (so that they wouldn’t try to harm him), which was accomplished when they saw the nobility giving him presents. Similarly, Taz suggests that Avraham had to ask for gifts in Egypt, and had do so through Sara, as otherwise there was no way to guarantee that she could tell everyone she was his sister without raising suspicion. If she wasn’t asked who he was, and instead voluntarily said “he’s my brother,” they would wonder why she was telling them that, and realize that he must really be her husband. Therefore, he had her ask people to help her poor brother (with the information that he is her brother being secondary), to which Pharaoh obliged. Otherwise, though, Avraham wouldn’t have accepted anything from Pharaoh either.
Later (on 14:23), Maharal elaborates on his first answer, differentiating between the King of Sodom making his offer to Avraham under distressful circumstances (Sodom being defeated and losing its possessions), which wouldn’t qualify as coming as part of G-d’s blessing, and Pharaoh’s gifts. He adds that giving Avraham gifts after being stricken by G-d for having taken Sara doesn’t qualify as “distress,” since the gifts were given as part of his atonement. Aside from trying to understand this difference, Avraham was given the gifts before Pharaoh was stricken, so shouldn’t have any “distress“ attached anyway. [This last issue also applies to Yad Malachi’s suggestion that Avraham wasn’t concerned that Pharaoh (or Avimelech) would say they made him rich, as they saw G-d defend Avraham (and Sara) when they were stricken, whereas the King of Sodom might not have realized that G-d was behind Avraham’s amazing military victory.]
B’er HaTorah (on 14:23; on 12:13 he quotes, and dismisses, an answer that is quoted by Sha’aray Aharon from a more recent source) explains Maharal’s distinction between distressful situations to mean that even though it was distressful for Pharaoh, his suffering brought honor to Avraham (and Sara), so qualified as a vehicle for G-d’s blessing. (Sodom’s suffering occurred independently of Avraham’s miraculous victory over the kings who had ransacked Sodom.) He then suggests his own answer, based on the inhabitants of Sodom being very miserly (a description backed by their unwillingness to share their bounty with others), a trait that stems from not believing that ultimately everything comes from G-d. Avraham knew they wouldn’t attribute these possessions becoming his as coming from G-d, but would always consider his wealth as really being theirs, so didn’t want to keep it (see 14:22, where Avraham made a point of mentioning that everything comes from G-d.)
B’er Basadeh (on 12:13) also makes a few suggestions, the first of which he subsequently found in kabbalistic sources, that the “gifts” Avraham wanted were the “unclean names” that would help him distinguish between holiness and “black magic” (see pg. 7 of http://www.aishdas.org/ta/5767/chayeiSarah.pdf), a gift he would pass on to his children (see Rashi on 25:6). However, these gifts seem to be “sheep, cattle, donkeys, servants and camels” (12:16), unless he obtained these things by invoking the help of the demons whose names he had just learned (see the “diyuk“ made by Ha’k’sav V’ha’kaballah on 12:16). B’er Basadeh’s second answer is based on the concept that what happened to our forefathers impacts what will happen to their descendants; even though Avraham would have normally refused to accepts Pharaoh’s gifts, he wanted to set the stage for us leaving Egypt with great riches. (Although he wasn’t told yet that we would be “strangers in another land for 400 years,” somehow Avraham knew, prophetically, that something like that would happen, and that the “other land” would be Egypt, and he wanted us to become rich there.) [I’m not sure why he didn’t also want to set a precedent of becoming rich from the spoils of war.]
On 14:23, B’er Basadeh says that the reason Avraham didn’t want to keep the spoils of war was so that no one would think the reason he risked his life (and the lives of those with him) by attacking the four kings was to recover the spoils for himself, rather than to save the life of his nephew (and the others who were taken captive). Da’as Sofrom suggests that the thought of gaining financially from war, which is ugly and messy by its very nature, was too distasteful for Avraham.
Maskil L’Dovid (on 12:13) says that Avraham wanted to become rich from G-d, not from people, but G-d’s promise only applied in the Holy Land. Therefore, when in Egypt, where there was no other option (because G-d hadn’t promised to bless him there), he accepted Pharaoh’s gifts, but when in Canaan, where he was confident that G-d’s blessing would be fulfilled, he turned down the king of Sodom’s offer. Nevertheless (as others point out), unless there was a reason why G-d’s blessing couldn’t be fulfilled through the property that had originally belonged to Sodom (and their sinful ways may be enough of a reason, as might the two points related in the previous paragraph), how could Avraham know that miraculously winning this war wasn’t the vehicle through which G-d was fulfilling His promise?
Netziv (on 14:23) suggests Avraham knew that Pharaoh (and Avimelech) felt honored that Avraham accepted their gifts, while the King of Sodom wouldn’t; he would feel that he was doing Avraham the favor. He adds that Avraham didn’t want to honor the King of Sodom by taking his gift, but (a) it really belonged to Avraham, so it wouldn’t have been a gift, and (b) even if it were, the King of Sodom wouldn’t have felt honored by Avraham accepting it.
In 5762, I pointed out that shortly after Avraham left Egypt laden with Pharaoh’s gifts (see 13:2), this sudden newfound wealth had led to the fight between Avraham’s shepherds and Lot’s shepherds, which caused their separation, including Lot separating himself from Avraham’s G-d (see Rashi on 13:11). I then suggested that this experience led Avraham to reconsider accepting things from others. [The original question is built on the assumption that Avraham had already reached his highest level by the time he moved to Canaan, so would react to similar situations in similar ways, but this is a fallacy. Hopefully we are all growing each and every day, and if Avraham was continually growing, he was not the same at 75 as he was when he was 85, or 99, etc. The fact that Avraham could do something differently when he was older and more experienced should not be a surprise.]
Another possibility is based on B’reishis Rabbah (51:6), which explains how Lot could have been described (19:29) as living in all the cities of the Sodom region (and not just in Sodom); either he owned so much that he had storehouses in all of them, or he lent money to all with interest (so had a hand in the business dealings of all the cities). If so, when their possessions were taken, much of it really belonged to Lot. This is especially true if the whole purpose of the attack was to capture Lot and his possessions in order to draw Avraham into a battle and kill him (see Pirkay d’Rebbe Eliezer 27), as Lot’s things were a primary target, and therefore constituted a large percentage of what the King of Sodom was offering Avraham. Even if Avraham would have taken gifts from others, how could he agree to keep what had been taken from his nephew Lot? By the same token, returning it to Lot himself would have been awkward as well, so he gave it back to the King of Sodom to be redistributed to their original owners (including Lot).