Melandri grabbed his and BMW's second victory of the season after passing Honda's Jonathan Rea on the final lap to take the chequered flag.
Race one winner Carlos Checa had earlier wasted a great opportunity to double up on the weekend when he crashed out while holding a comfortable lead.
Checa raced into a 1.7 second lead after just five laps but threw it away with a mistake that sent the Althea Ducati rider flying.
With the Catalan out of the running, it was left to Melandri, Rea, Max Biaggi and Chaz Davies to battle for first place.
Melandri seized the initiative but was overhauled on lap 11 of 18 - the race was shortened following an oil spill - by Rea, who remained in the lead until the final lap.
The straight-line speed of Melandri's BMW proved the difference and the Italian held on to secure victory, despite Rea's best efforts late on.
"It was such a difficult race for me, I was lucky for sure when Carlos made a mistake," Melandri said. "I was struggling a little bit with a full fuel tank but after 50 per cent of the race it was getting better.
"When Johnny passed me he was much faster than me, and I lost a bit of concentration, so Max passed me and then Chaz Davies did as well.
"So I tried to stay calm, take my rhythm and try to get a good feeling to catch Johnny. I was very lucky because my engine was so strong so I could pass him on the straight.
"Otherwise for me it would have been really difficult to pass him. So a podium in race one and a win in race two is really unbelievable."
Biaggi completed the podium and the Italian was the weekend's big winner: his two third-places reaped 32 points and he now leads the championship by 18 points.
"To come all the way to Utah and finish on the podium twice is not so bad, but my dream was to win a race," said Biaggi. "But there were some other guys who did a better job than me so third position is not so bad. I am not super happy, but not so bad."
Biaggi's Aprilia counterpart Davies came in fourth with Tom Sykes of Kawasaki fifth.
Race two result:
1. Marco Melandri ITA BMW Motorrad S1000RR 32m 56.257s
2. Jonathan Rea GBR Honda WSBK CBR1000RR +0.195s
3. Max Biaggi ITA Aprilia Racing RSV-4 +2.137s
4. Chaz Davies GBR ParkinGO MTC Aprilia RSV-4 +4.245s
5. Tom Sykes GBR Kawasaki Racing ZX-10R +9.534s
6. Eugene Laverty IRL Aprilia Racing RSV-4 +9.798s
7. Davide Giugliano ITA Althea Ducati 1098R +11.891s
8. Leon Haslam GBR BMW Motorrad S1000RR +12.715s
9. Jakub Smrz CZE Liberty Effenbert Ducati 1098R +13.017s
10. Sylvain Guintoli FRA Effenbert Liberty Ducati 1098R +13.703s
- Related links