Federal Reserve officials told Goldman Sachs Group Inc. and Morgan Stanley that they were about to flunk a portion of the annual stress tests but offered them a deal to avoid an outright fail and continue paying billions to shareholders.

In phone calls to executives of the Wall Street titans on June 21, regulators told them that to fully pass the test, they would have to cut almost in half the combined $16 billion they had hoped to pay out to shareholders, according to people familiar with conversations between the Fed and...