Castroneves was attempting to become the first driver to win the race three years in a row, but De Ferran edged his fellow Brazilian by 0.2990 seconds and pushed team owner Roger Penske's record for victories at the Indianapolis Motor Speedway to 13.
"The real thing that helped us was our fuel economy," Penske said. "It kept us in great track position. The No. 13 is 13, but it was lucky for us today. It's great to see Gil finally get into victory lane here."
It was easily the biggest victory of de Ferran's career as he got to chug from the traditional bottle of milk in victory lane.
"I love milk," said de Ferran, who led the final 31 laps of the race. "It's hard to describe in words what I'm feeling now," de Ferran said. "Whenever you win a race like this, you want to say something smart, but it's unbelievable right now."
With that, de Ferran broke down in tears in victory lane as he hugged his wife, Angela.
"It was an excellent result for Team Penske. Unfortunately I didn't win, but it was a great result for the team," Castroneves said. "The yellows kept coming toward the end, but the car was excellent."
Tony Kanaan made it a Brazilian sweep of the top three spots. Tomas Scheckter of South Africa and rookie Tora Takagi of Japan rounded out the top five.
Alex Barron of the United States was sixth.
A dramatic crash involving rookie Dan Wheldon on lap 187 set up the race to the checkered flag. It started when Sam Hornish Jr. made a run on Wheldon down the backstretch. Wheldon's car blocked Hornish, sending him toward the grass.
As Wheldon went into Turn 3, his entry was too low and that sent the rookie from England slamming into the outside wall. The car, while airborne, flipped and landed upside-down before skidding to a stop in Turn 4.
Wheldon was moving the car as safety crews arrived. They manually turned the car right-side up and Wheldon was able to climb out.
The yellow flag waved with de Ferran in front of Castroneves, Kanaan, Scheckter, Takagi and Hornish.
Castroneves was in the lead when he made his final pit stop of the race on lap 167. He made the stop in tandem with his teammate, de Ferran, and Castroneves was first out of the pits.
De Ferran passed Castroneves for the lead on lap 170 before a yellow flag waved when NASCAR Winston Cup driver Robby Gordon's car had a gear box failure two laps later.
De Ferran was ahead of Castroneves on lap 175 when Scott Sharp lost control of his car in the apex of the first turn and slammed into the outside wall in virtually the same area where he crashed on the first lap of the 2001 Indy 500.
With de Ferran in front of Castroneves on lap 180, Wheldon's car became the second to go airborne at the Indianapolis Motor Speedway in the last two months. Retired driver Mario Andretti had a spectacular airborne crash on April 23 while testing for his son, Michael's, IRL team.
On lap 191, Scott Dixon spun out and crashed on the frontstretch. The yellow flag stayed out until lap 194, when the green flag waved and de Ferran pulled away from Castroneves.
The two Brazilians continued their duel through the 196 laps when the Chevrolet engine in Sam Hornish's car blew. The two-time defending IRL champion was able to pull off the track without causing a caution.
Notable deaths of 2014 [PHOTOS]