Lincecum had asked for $21.5 million in salary arbitration, and the Giants had offered $17 million in what would have been a one-year contract if the sides had gone to a hearing in February.
The deal also buys out the right-hander's final two years of arbitration eligibility but allows the four-time All-Star to become a free agent when first eligible following the 2013 season.
Lincecum went 13-14 with a 2.74 ERA in 2011. He struck out 220 batters in 217 innings pitched, walking 86.
In his five-year career, Lincecum has compiled a 69-41 record with a 2.98 ERA in 156 games, all but one of which have been starts.
Lincecum will be the second-highest paid Giants player in 2012. Left-hander Barry Zito will have a $19 million salary.