James Dimon posted his first quarterly loss as chairman and chief executive of J.P. Morgan Chase & Co. after the New York bank set aside more than $9 billion to cover a tangle of legal woes.

The money is expected to help pay for a potential record-breaking settlement with the U.S. government over mortgage securities issued as far back as 2005. J.P. Morgan and the Department of Justice remain far apart on a deal but have discussed...