"It's a dream come true," said Hunter-Reay. "I can't even believe it."
Hunter-Reay led from a restart with six laps remaining after the race was halted to clear debris off the track.
Castroneves passed for the lead on the inside entering turn one with five laps to go, but Hunter-Reay regained first position in the third turn of lap 197 with a daring inside move.
The Brazilian reclaimed the lead on the first turn of lap 199, only to lose it at the start of the next lap as Hunter-Reay went to the outside and successfully passed Castroneves to win.
"It was a fantastic fight to the finish," Hunter-Reay said.
"We all raced each other clean but really hard.
"I was just biding my time and waiting for the right opportunities."
Castroneves could only smile and shake his head.
"They did an outstanding job. It was close. It's a shame so close but today was Ryan Hunter-Reay's day," he said.
"It was a great fight. I was having a great time.
"Unfortunately, second. It's good, but second sucks."
US racer Marco Andretti, the grandson of 1969 Indy 500 winner Mario Andretti, could not end the family's 45-year Indy jinx, settling for third.
- AAP