(Reuters) -- The San Francisco 49ers won a thrilling divisional playoff over the New Orleans Saints 36-32 on Saturday to reach the NFC Conference championship for the first time since the 1997 season.

In a dramatic finale, the lead changed four times in the last four minutes before the 49ers emerged victorious thanks to a 14 yard touchdown pass from quarterback Alex Smith to Vernon Davis with just 14 seconds to go.

Tight-end Davis ended the game with 180 yards receiving and two touchdowns while Smith put up 299 yards and threw three touchdowns.

The Saints, Super Bowl champions two seasons ago, came back from a 17-0 deficit and five turnovers to lead on two separate occasions in the last four minutes only to suffer their first loss in 10 games.

The 49ers took an early grip on the game with two touchdowns in the first quarter as the Saints struggled to cope with the early pressure, giving up three turnovers.

A field goal at the start of the second quarter gave the home side a 17-0 lead but the Saints mounted a strong response with Brees leading his team to touchdowns on two consecutive drives.

A high 14 yard pass to Jimmy Graham in the end zone and then a brilliant 25 yard pass to Marques Colston, superbly caught by the receiver, brought New Orleans within three points at half-time.

San Francisco stretched their lead to 20-14 at the end of the third quarter with a David Akers field goal before the dramatic final quarter.

John Kasay and Akers traded field goals before the Saints took the lead for the first time when Darren Sproles ran in from 44 yards.

Smith responded with a classic quarterback sweep to put the 49ers back in front with 2:18 left then Brees found Graham with a score that, along with a two point conversion, put the Saints five points up with 1:48 to go.

But Smith and Davis combined again to send the packed crowd at Candlestick Park into delirium and take the 49ers into next Sunday's championship game against either the Green Bay Packers or the New York Giants.

(Reporting by Simon Evans in Miami; Editing by Julian Linden)