In a surprising move, the Giants agreed with right-hander Tim Lincecum on a two-year contract on Tuesday, preventing the two-time National League Cy Young Award winner from entering free agency.