The two Mercedes were predictably in a different league to their rivals and recorded their second one-two finish in eight days after Hamilton's victory last weekend in Malaysia.
With no team orders, the two were allowed to race for the lead and there were several close calls when the pair came inches away from colliding, providing a thrilling spectacle under the lights at the Bahrain International Circuit.
"It was very, very fair and it was very hard to keep him behind," Hamilton said. "He was very fast on the option tires and I was on the knife-edge the whole time. ... Me and Nico haven't had a race like that since back in our karting days. In our first (karting) race, he led all day and I overtook him on the last lap and won, and I thought for sure he is going to do that to me today."
Daniel Ricciardo finished fourth, getting the better of his Red Bull teammate and defending four-time F1 champion Sebastian Vettel, who finished sixth. Vettel suffered the ignominy of being ordered by his team to let the quicker Ricciardo pass when they were fighting for position early in the race.
Williams drivers Felipe Massa and Valtteri Bottas had looked poised to challenge for a podium place but they were hurt by the timing of a late safety car and finished seventh and eighth, with the Ferraris of Fernando Alonso and Kimi Raikkonen filling the final two points positions in ninth and 10th.
The race was closely fought up and down the field throughout, providing a boost for the sport after a tepid opening two races of the season, and the safety car set up a nail-biting shootout for the final 11 laps.
Hamilton was ahead but on the harder of the two tires while Rosberg was on the softer rubber. While behind the safety car, both drivers were cautioned by team racing director Paddy Lowe to race fairly and ensure both cars made it to the finish.
Twice Rosberg was able to pull off passing moves at the end of the main straight, but both times Hamilton was able to get better drive out of the ensuing corners and narrowly stayed ahead in some enthralling wheel-to-wheel racing. Rosberg's tires began to wear out and he was not able to mount a challenge over the final three laps.
"It was the most exciting race I have done in my career," Rosberg said. "Lewis did a great job defending -- it was a massive fight. I thought I'd got him about nine times but it didn't work out; he always got the run back on me."
Rosberg did have the comfort of staying ahead in the drivers' championship with 61 points compared to Hamilton's 50.
The safety car came out after Lotus driver Pastor Maldonado emerged from the pits and plowed into the side of the Sauber of Esteban Gutierrez at the first corner, flipping the Mexican's car in a frightening accident, but Gutierrez was able to walk away.
Maldonado was judged by race stewards to have caused the incident and given a stop-go penalty.