Yes, legally the PD can prevent you from leaving because you signed the contract. It depends on your PD though.
The answer is actually a bit more complicated.
Residency is a job. You can quit anytime you want, contract or no contract.
The contract will state in it what the terms are for termination. Usually it defines a certain amount of notice -- let's say it's 90 days. Thus, in order to "satsify" the contract you would need to give 90 days notice, presumably from the start date of the contract (July 1). So, you could "legally" quit at the end of September.
What happens if you simply quit now and never show up? Theoretically, your program could sue you for damages -- costs of hiring someone new, moonlighting shifts, etc. Whether they could win in court is unclear. The likelihood of this happening is very low.
However, they could consider this unprofessional behavior. As such, they could 1) make your life miserable for the next month, 2) give you an unsatisfactory professional evaluation to your Board for your intern year (in IM, this would make you entire intern year "not count"), 3) You'll need to get a statement of your training from them for every job you get from here on in, and there would likely be some statement about the professionalism issues stated. Whether this would impact your future hiring is unclear.
Also, there's the big problem not mentioned yet. There is a good chance that you'll need to resign youtr current position to even look at the new position. Your current PD will need to start looking for a replacement, and so will likely require that you simply resign so they can, in good conscience, look for a replacement. If you don't get the other spot, you might have nothing.