Lynch signed the bill after legislators approved it, after he had promised a veto if the law didn't clearly spell out that churches and religious groups would not be forced to officiate at gay marriages or provide other services.
The Senate passed the measure Wednesday, and the House — where the outcome was more in doubt — followed later in the day. The House gallery erupted in cheers after the 198-176 vote.
U.S. & World
Stories that affect your life across the U.S. and around the world.
"If you have no choice as to your sex, male or female; if you have no choice as to your color; if you have no choice as to your sexual orientation; then you have to be protected and given the same opportunity for life, liberty and happiness," Rep. Anthony DiFruscia, R-Windham, said during the hourlong debate.
New Hampshire's law takes effect Jan. 1. Massachusetts, Connecticut, Maine, Vermont and Iowa already allow gay marriage, though Maine opponents hope to overturn that state's law with a public vote.
California briefly allowed gay marriage before a public vote banned it; a court ruling grandfathered in couples who were already married.
New Hampshire opponents, mainly Republicans, objected on grounds including the fragmented process that required three bills.
"It is no surprise that the Legislature finally passed the last piece to the gay marriage bill today. After all, when you take 12 votes on five iterations of the same issue, you're bound to get it passed sooner or later," said Kevin Smith, executive director of gay marriage opponent Cornerstone Policy Research.
Lynch, a Democrat, personally opposes gay marriage but decided to view the issue "through a broader lens."
The revised bill added a sentence specifying that all religious organizations, associations or societies have exclusive control over their religious doctrines, policies, teachings and beliefs on marriage.
It also clarified that church-related organizations that serve charitable or educational purposes are exempt from having to provide insurance and other benefits to same sex spouses of employees. The earlier version said "charitable and educational" instead of "charitable or educational."
The House rejected the language Lynch suggested two weeks ago by two votes. Wednesday's vote was on a revised bill negotiated with the Senate.
The vote was supporters' last chance this year in New Hampshire.