Overall Formula One series leader Lewis Hamilton of McLaren will start from third while Kimi Raikkonen had the fourth-best qualifying time in his Ferrari. McLaren's Heikki Kovalainen rounded out the top five for Sunday's race at the Bahrain International Circuit.
Kubica snapped a Ferrari-McLaren monopoly on the top spot that had lasted 22 races. Fernando Alonso's pole for Renault at the Chinese GP in October 2006 was the last for another team.
Kubica clocked 1 minute, 33.096 seconds in the final 10-minute session. Massa, who won both of Friday's practice sessions, had 1:33.123.
Kubica, who has eight championship points, finished second at the Malaysian GP last month.
Hamilton leads the drivers' standings with 14 points, three more than world champion Raikkonen and Nick Heidfeld of BMW Sauber. Heidfeld placed sixth in qualifying.
Massa, with zero points from the first two races, got stuck behind traffic as he went out for his final qualifying lap.
The Brazilian, who claimed the pole here last year on his way to winning the race, started first at Sepang but retired after spinning out when he was in the lead. Raikkonen eventually won.
Teams went to a lighter fuel load and softer tires by the second session with the track temperature peaking at 111 degrees. But gusting wind and cloud cover lowered the temperature for the final session, affecting teams' strategy.
The lack of traction control left drivers needing to exercise caution at the curvy Bahrain International circuit.
Hamilton, who won the season-opening Australian GP, wobbled on his opening qualifying lap after crashing in practice on Friday.
The 23-year-old Briton lost control through a tight series of turns Friday before sliding sideways into the protective barrier. McLaren's crew worked overnight to repair both right wheels and other damage to the car.
Red Bull's David Coulthard, who was fifth in practice earlier Saturday, failed to get out of the first qualifying session.
Super Aguri's Takuma Sato spun out during the first session, as the rear of the Japanese driver's car clipped the protective barrier to end his day.
Rosberg was fastest at the final practice session earlier Saturday, more than two-tenths of a second ahead of Massa.