The ruling marked an initial victory for shareholders in what figures to be a long-running battle with the U.S. government over its 2012 decision to require Fannie and Freddie to send all of their profits to the government as part of the mortgage companies’ 2008 bailouts.