From what I read, Apple isn't agreeing to, they may be compelled to by the judge. You are correct, apple isn't selling users the software on the devices, they are licensing it, and the terms of that license likely stipulates that users have no right to the code (I don't know for sure, because I've never read the license, even as I type this from my iPhone).
In this case the license isn't the issue. The court isn't bound by the license, and if it deems it necessary for evidentiary purposes, over and above any objections Apple raises, then it will order Apple to turn over the code.