This is a plot hole as confirmed by one of the writers of the Endgame. Russo brothers knowingly kept the scene to cheer the fans.
From the link:
Avengers: Endgame co-writer Christopher Markus has shed some light on a plot hole from the film regarding Captain America using Thor’s hammer, Mjolnir.
While Cap finally lifting up the iconic hammer was a crowd-pleasing moment, some fans called into question exactly how Cap used it to call down the lightning on Thanos. As we learned in Thor: Ragnarok, the hammer is merely a conduit for Thor’s true power.
“Are you Thor, the God of Hammers? That hammer was to help you control your power, to focus it. It was never your source of strength,” we heard Odin explain to his son.
So if the lightning comes from Thor’s godhood, not the hammer itself, how did Cap summon it? Turns out there’s no good explanation.
“There was certainly a debate at one point because particularly in Ragnarok, it establishes that Thor can summon the lightning without the hammer. I think Odin even says, ‘It was never the hammer.’ And yet Cap summons the lightning with the hammer. You get to those things and you’re like, ‘It’s too awesome not to do it! We’ll talk about it later,’” Markus explained to SlashFilm.
But of course the filmmakers were all on board with breaking the rules if it meant delivering a rousing moment for the audience. Composer Alan Silvestri chimed in with an anecdote on the effectiveness of the moment.
“The thing that Joe and Anthony struggled with, and I think did a magnificent job of solving, was how high do we let the audience get knowing that we’re really going to pull the rug out? That moment when Cap gets the hammer, we were in Westwood on preview night the Thursday night before opening night. When that happened, that place stood on its feet and screamed. It was the biggest reaction in the movie,” Silvestri said. “It was very tempting to give it all away [by using the full Avengers theme at that moment], but what we knew was, we’re about to destroy Cap little by little, and we want everybody to be right there, that this is hopeless because the real moment is the reuniting of all of the Marvel universe to help him. So that was one of those brilliant Joe and Anthony decisions. ‘No, don’t take it. Leave it over there. Resist.’”
The article further clarified why the other answer is wrong:
As some have pointed out in the comments, this may not be a plot hole if you consider the original enchantment Odin put on Mjolnir in the first Thor movie. Odin's word were, "Whosoever holds this hammer, if he be worthy, shall possess the power of Thor." Given that Thor's powers include lightning, it would stand to reason that Cap would gain that power when he wields Mjolnir in Endgame. However, as Markus explains it, Ragnarok changed the rules on how we understand the relationship between Thor and Mjolnir, so perhaps the "power of Thor" only pertains to his godly strength and durability, which Cap clearly demonstrated as he brought the fight to Thanos. Ultimately, Markus' statement paints the picture that Odin's original enchantment does not include Thor's lightning powers, which is why Markus considers it a plot hole.