BEIJING ( AP ) - Michael Phelps set a world record Sunday to win his first gold medal of the Beijing Olympics , beating rival Ryan Lochte in the 400-meter individual medley with a time of 4 minutes , 3.84 seconds .