I have encountered this thought problem before, and while you accurately describe instinct, I tend to find a different calculus to be more moral.
As I see it there are two basic levels of responsibility regarding children. First, it is generally understood that a parent will do everything within their power to protect their child. Second, as I understand it every adult has a responsibility to protect all children, to an extent (which possibly doesn’t include risking harm to oneself or others).
When your friend lets you take their child to the beach, your friend is delegating their responsibility to you. The unspoken (or perhaps spoken) words are “you can bring my girl to the beach, watch and protect her on my behalf, as if she were your own child”. At this point you have incurred two additional responsibilities: you are responsible to your friend for keeping your promise, and you are responsible to the girl for protecting her.
In our unfortunate hypothetical both children are drowning. You are responsible to both for their protection, as if they were each your child. But you are also responsible to your friend for keeping a promise, and this tips the scales in favor of saving her daughter first.
The argument can be raised that you have a responsibility to your spouse and family for protection of your own child, but I must add that this same responsibility is incumbent upon you regarding your friend’s spouse and family. A utilitarian might reach a different conclusion depending on the number of people affected by each child’s death, if there is time to consider that.
That being said, in the heat of the moment I may very well forgo any rational thinking and go straight for my kid. I wouldn’t blame anybody for doing the same.
~Max