Hamilton, who also won last weekend in Malaysia, finished 1.085sec ahead of Rosberg in another triumph for the Mercedes team who have won all three races this season. (Agencies)
Sergio Perez in a Force India was third, a huge 24.067sec off the winner's time, to give his team just their second podium finish.
Red Bull's Daniel Ricciardo, who had started in 13th place, was fourth, followed by Nico Hulkenberg in the second Force India and world champion Sebastian Vettel in the second Red Bull completing the top six.
The result left Rosberg, the Australian Grand Prix winner, still on top of the world championship standings with 61 points. Hamilton has 50 with Hulkenberg in third place on 28 points.
The British driver had been cruising to victory in the 900th ever grand prix with a lead of more than nine seconds when the safety car was called out on lap 42 of the 57-lap floodlit race.
Rosberg, who had been on pole, then briefly slipped past the Briton but Hamilton reasserted his authority under braking despite his medium tyres lacking the grip of the German's softer options.
On the final lap, Hamilton consolidated to take his first win in Bahrain after a memorable close-quarters battle.
"It's great to see that we have such a great following here, I am so grateful to the team for getting us up here and this is my first win here in Bahrain which is nice," said Hamilton.
"It was really exciting, Nico drove fantastically well, it was so fair but it was so hard to keep him behind me, I was on a real knife edge at the end but just managed to take it."
Despite Rosberg's second place, the German was despondent he could not retake the lead in the closing stages.
"It's very disappointing. I hope to get it right next time," said Rosberg, who hit back at critics who claim the dominance of Mercedes was bad for the sport.
Hamilton, who also won last weekend in Malaysia, finished 1.085sec ahead of Rosberg in another triumph for the Mercedes team who have won all three races this season.