The Brazilian midfielder headed Balde Keita's cross past helpless Legia goalkeeper Wojciech Skaba in the 53rd minute to earn his team all three points.
And Hernanes was unlucky not score his second after the hour mark when his powerful effort struck the post with Skaba already beaten.
Lazio coach Vladimir Petkovic opted to rest midfielders Antonio Candreva, Cristian Ledesma and Senad Lulic, as well as forward Miroslav Klose, in view of Sunday's capital-city derby against Roma.
The Poles, who will also face Apollon Limassol and Trabzonspor in Group J, showed some resolute defending in the opening half.
In the 20th minute, Legia defender Junior made a crucial clearance at the far post from Hernanes' goal-bound shot.
Midway through the first half, Lazio keeper Federico Marchetti parried away Jakub Kosecki's poweful strike from 15 yards.
Before the break, Legia defender Lukasz Broz hooked Luis Cavanda's dangerous cross to safety with Ederson waiting at the far post.
The pressure from the home side intensified after the restart and in the 47th minute Lorik Cana's header from Hernanes' corner went just wide.
Lazio broke through soon after when Hernanes easily slipped his marker and nodded the ball past Skaba to bring the Stadio Olimpico to life.
In the 67th minute, a Lazio counter-attack left Hernanes in the area again but this time he drilled a shot against the post.
Two minutes from time, Skaba made an instinctive save to deny substitute Diego Novaretti's header.
Legia pressed forward in the last stages of the game in the hope of finding a late equaliser but were unable to break Lazio's disciplined defence.