The Disney Aladdin Live Action Novelization (2019) clarifies this a bit:
Unconscious, Aladdin didn't see the Genie suddenly appear. Nor did he see him hesitate, confused to find himself underwater.
But it took only a moment for the powerful being to figure out where he was, and when he spotted Aladdin, panic filled his face.
The Genie rushed forward, shaking him. But Aladdin didn't respond. "Kid, I can't help you with this one," he said desperately.
"You gotta wish!" He poked at Aladdin's floating body. Still nothing. "Come on! Say the words. 'I Aladdin, being of sound mind
and body, do wish-'" He stopped. It was no use. Reaching out, he squished Aladdin's lips together, forming the words with his
fingers. He leaned closer, hoping for something. But all he got was silence.
In that moment, the Genie did something that, if Aladdin had been awake, would have surprised and touched him. He pretended he
did hear something. Something specific. Still moving Aladdin's lips, he spoke for him. "My second wish is to be saved from
certain doom at said time of happening..." The wish made, Genie grabbed Aladdin and rocketed toward the surface.
The Genie doesn't want Aladdin to die, and is stretching the definition of making a wish; in the movie he says
Okay. Okay. A little gray area here, but it's worth a try.
The novelization explains that the Genie does this purely out of friendship for Aladdin:
The Genie had saved him.
Despite his rules about favors and friendship, the Genie had somehow, for some reason, managed to pull him out of the water and save his life. There was nothing he could say or to do ever repay him. But he could try.
Gesturing to him, Aladdin struggled to speak. But his throat was raw and the effort was painful. He crooked a finger, trying to get the Genie to lean in still closer. Rolling his eyes, but doing as asked, the Genie bent down. "Thanks, Genie," Aladdin whispered.
To his surprise, the Genie looked embarrassed. "Hey, no problem," he said, trying to brush it off. "I was in the area..."
Aladdin shook his head. "I thought you said no favors."
"Well, technically it wasn't-" the Genie countered.
"Thought you said no friends," Aladdin said, getting enough life back in him to enjoy teasing the Genie a bit.
"It actually cost you a wish," the Genie said, trying to gain back the upper hand. But it didn't matter. They both knew what he had done-and what it meant."