Captain America the hero was never truly worth of wielding Mjolnir, it was Steve Rogers the person who gained worthiness in Avengers: Age of Ultron, tying both stories together in a more subtle way.
Immediately after the movie released, it was argued among fans - and even among the movie's creators, given that the Russos and writers Christopher Markus and Stephen McFeely fundamentally disagreed with each other - whether or not Avengers: Infinity War - and not as Captain America.
Captain America has always been a flawed superhero, because of his deep-seated origins as a political hero in both the comics and movies. He was envisioned as being perfect soldier and it showed in both five years between Infinity War and Endgame, Rogers learned the true cost of loss, and he struggled with it. But it was that ability to overcome it all, to truly become a hero, that made him worthy of Mjolnir.
Captain America's black-and-white, good-vs-evil worldview is something he challenged in years between Civil War and Infinity War, because he couldn't let go of what it meant to be a hero. Without a war, could he rest?
When Thanos and his Black Order made their move to gather the Infinity Stones, that's when Rogers attempted to resurrect his identity as Captain America (to an extent) - but while the shield and suit were, and always will be, perfectly fit for Steve Rogers, he outgrew what both represented: victory. Endgame wasn't about winning the battle or beating Thanos; they already tried to do that. It was about saving those they lost - that's what made him a real hero.
Funnily enough, the differences between Captain America the hero and who Steve Rogers had become in Endgame were on display when the two met in 2012 - when Rogers returned to the Battle of New York. Captain America's bombastic attitude even turned off 2023's Steve Rogers; though the moment was played off as a joke, there was a deeper meaning of growth behind it all. Captain America couldn't lift Mjolnir in Age of Ultron because he hadn't grown enough yet; he was always a sound strategist, but he hadn't developed a pragmatic, grounded temperament that he needed to be worthy. Even at the start of Endgame, Rogers wanted revenge - that wasn't the hero's way. It was the choice to risk everything, to save everyone - everywhere - that put him on the right path. Thor understood this, and he went through that journey himself in his own movie; it just took Steve Rogers a little longer.