WASHINGTON—President Barack Obama secretly wrote to Iran’s Supreme Leader Ayatollah Ali Khamenei in the middle of last month and described a shared interest in fighting Islamic State militants in Iraq and Syria, according to people briefed on the correspondence.

The letter appeared aimed both at buttressing the campaign against Islamic State and nudging Iran’s religious leader closer to a nuclear deal.