Ibn Hajar al-Haythami was asked this question and he replied:
Two views were mentioned in al-Majmoo’, in (the chapter called) al-Muqaddam, and I do not know of anyone who stated that one of them was more correct than the other. The one which appears to be more correct is that he should give water to the one who is thirsty at that time, if there is the fear that he will die of thirst, because his death is certain, unlike the one who has the water, who has the chance of finding more water later on. But if he is in an arid land where he despairs of finding water and he thinks it most likely that death is inevitable if he gives away the water that he has with him, then the matter is subject to further discussion, and it is more likely that giving the water in this case is not obligatory. The same applies if the thirsty person fears that he may lose a limb or become sick etc. because of his thirst, and the one who has the water fears that he may die later on, then it is also more likely to be correct that he does not have to give away the water.