Yes it is primarily a moral obligation which has been brought down among seafarers for generations. The legal obligation is more circumspect and both are not always followed.
In 1979 I was Captain on a Drillship operating in the South China Sea, when the big exodus from Southern Vietnam happened. It was estimated that 40,000 people was afloat in the area in the period between the monsoons.
We were sitting on 8 anchors in the shipping lane in the disputed zone between Indonesia and Vietnam and unable to avoid the refugee boats, while watching ships of all nationalities giving them a wide berth around us.
During a single hitch in June-July 1979 we picked up abt. 2,200 people from boats that came for us like flies to a lump of dung, while other vessels were avoiding them like pests.
Both the Operator and the Indonesia authorities instructed me to not do so, but I insisted on following the time honoured maritime tradition of assisting people in distress, even if it was self-inflicted. (They axed holes in the boat when they got close)