Front line German subs and early US subs used a clutch system to engage and disengage the diesels from the screws when switching between surface and submerged propulsion modes.
US fleet boats used a form of the Alphine-MacPherson (I think that's what they called it... have to check my sources when I get home

) drive pioneered on the USS Jupiter (later USS Langley) in 1912: internal combustion engines turned a generator which powered electric motors which turned the prop shafts. Batteries are cut in, in place of the diesels, when submerged.