When Ukrainian National Guard Lt. Andriy Kulish ambushes Russian forces, he thanks the Canadian Army.

The Canadians trained Lt. Kulish’s Rapid Response Brigade last summer in urban warfare, field tactics and battlefield medicine. The exercise in western Ukraine was one of the many in recent years with troops from Canada, the U.K., Romania and the California National Guard.

This was just one piece of a little-publicized effort by countries of the North Atlantic Treaty Organization that transformed Ukraine’s military up and down the ranks, from foot soldiers to the defense ministry to overseers in parliament. It is one big reason why Ukraine’s nimble fighting force has surprised the world by fending off a much larger and better-equipped invading army, say Ukrainians and their Western advisers.

Through classes, drills and exercises involving at least 10,000 troops annually for more than eight years, NATO and its members helped the embattled country shift from rigid Soviet-style command structures to Western standards where soldiers are taught to think on the move.

In confounding Russian invaders today, Lt. Kulish says his comrades-in-arms “are definitely using procedures they learned during the training with NATO.”

The Western assistance, while never secret, wasn’t trumpeted to avoid riling Russia. It also remained low-key because it was a valuable source of intelligence for the U.S. and its allies. Ukraine has been fighting a shooting war with Russian-backed separatists in parts of its east for years, meaning Kyiv fields some of Europe’s most battle-hardened soldiers. Their front-line experience made them sponges for NATO training—and offered NATO commanders a window into what it would be like to fight Russia, say Western officers involved in the programs.

A Canadian instructor observed Ukrainian servicemen during a military exercise at the Yavoriv training center in 2016.

Photo: markiian lyseiko/EPA

By the time Russia invaded on Feb. 24, training of Ukrainian forces had become so extensive that, although at least eight NATO countries participated, much of the hands-on training was being done by Ukrainian instructors. To NATO commanders, that was a sign Ukraine had internalized their teachings.

“The lesson learned is that support and help over many years had a significant impact,” says NATO Secretary-General Jens Stoltenberg.

NATO’s work in Ukraine was also more successful than comparable Western efforts in Iraq and Afghanistan. Advisers credit this to Ukraine’s relatively cohesive society and a recognized central government supported by bureaucracies that, while often inefficient and plagued with corruption, still embodied a unified state. Perhaps most significant, Ukraine had a clear foreign enemy to fight following Russia’s 2014 seizure of the Crimean Peninsula and military support for a rebellion in the country’s east.

Russian President Vladimir Putin, in launching his invasion of Ukraine, cited the country’s possible membership in NATO as a reason for attacking. Ukrainian President Volodymyr Zelensky has since proposed renouncing that ambition. Whatever the outcome, say Ukrainians and Western advisers, Kyiv’s forces have learned to wage war along NATO’s rules, and are showing it with battlefield successes.

Ukraine’s skirmishing units are the spearhead of a completely rebuilt military establishment. NATO advisers brought with them concepts novel to Ukraine’s Soviet-style force including civilian control of the military, professional inspectors, external auditors and logistics specialists.

Abandoning the emphasis on numbers of soldiers and weapons, NATO advisers instilled the concept of capacity building, where commanders set goals and ensure they have troops and weapons needed to achieve them.

Ukrainian servicemen on a destroyed Russian tank outside Kyiv on March 31.

Photo: Vadim Ghirda/Associated Press

Ukrainian servicemen near Kyiv on March 30.

Photo: Shutterstock

To advance the approach, NATO introduced the idea of noncommissioned officers: experienced soldiers promoted to ranks of authority who serve as vital links between top brass and ground troops. NATO countries also helped Ukrainian military leaders adopt an approach called mission command, where higher-ups set combat goals and devolve decision-making as far down the chain of command as possible, even to individual soldiers.

In the Soviet approach, still widely used by Russia, senior officers give orders that foot soldiers have little room to discuss or adapt.

“That made a big, big difference,” says former defense minister Andriy Zagorodnyuk. “NCO reform and mission command raise the effectiveness of your forces by many times.”

Lt. Kulish says the training is doubly effective because Ukrainians know Soviet military thinking.

“The Russians are using their typical tactics, which haven’t much changed since Stalin’s times,” he says. First comes artillery salvos. “Then they throw loads of meat to take our positions,” he says, referring to Russian soldiers. The Ukrainians, in contrast, are unpredictable and agile. “We bring chaos to their ranks,” he says.

The work to develop those skills began inauspiciously in 2008. Russia had invaded Georgia, prompting NATO to extend to it and Ukraine vague invitations for membership. The alliance drafted a 70-page action plan spelling out “Ukraine’s strategic course of Euro-Atlantic integration,” essentially a road map for Kyiv to meet NATO’s democratic standards, including a more professional, civilian-controlled military. Those efforts gained little traction due to weak support in the West and resistance within Ukraine’s still-Soviet-style military.

Russia’s routing of Ukrainian forces in 2014 jolted Kyiv. Then-President Petro Poroshenko ordered a military transformation, energizing the NATO effort. Western officers focused their attention on a 150-square-mile military training facility in the city of Yavoriv, 10 miles east of Ukraine’s border with Poland, which itself had transformed into a postcommunist leader within the alliance.

In a sign of how significant the Yavoriv center eventually became, Russia in mid-March targeted it with a missile strike, killing 35 people.

Ukrainian servicemen carried a coffin on March 15 for a man who died in the bombing of Yavoriv.

Photo: Alexey Furman/Getty Images

Mourners at the funeral for servicemen killed in the bombing of Yavoriv.

Photo: Bernat Armangue/AP

The first priority in 2014 was helping Ukrainian troops fight Russian proxies in the east. NATO launched courses in battlefield medicine, civil emergency planning and countering Russian hybrid warfare, from drones to phone-hacking. Western officers began drilling Ukrainian National Guard troops in modern combat tactics, prompting Ukrainian Army officials to request similar training, recalls a senior U.S. defense official.

In Kyiv, government officials worked with NATO advisers to prepare deeper changes. Advisers from the U.S., U.K. and other NATO countries explained that to make Ukraine’s military more effective, its entire management had to change.

Advisers found problems at all levels, such as parallel military and civilian medical systems that required an act of parliament to be permitted to cooperate, recalls retired Col. Liam Collins, a former U.S. Army Special Forces officer.

U.S. officials repeated defense department mantras, like “It’s not the plan, it’s the planning.”

When Soviet-trained officers and bureaucrats resisted changes, advisers tried to work around them, says Kristopher Reeves, a Canadian Army colonel who led his country’s branch of the training from 2017 to 2018.

“We focused on leaders who could use our energy and multiply it,” says Col. Reeves.

By the time he left, training sessions at Yavoriv had grown from companies of 150 soldiers to battle groups entailing more than 400 troops. Ukrainians began replacing Western soldiers in leading combat simulations.

“The second-best thing to being in combat is teaching it,” says Col. Reeves, of how militaries learn.

Annual exercises orchestrated at Yavoriv by the U.S. Army, dubbed Rapid Trident, let Ukrainian troops practice with forces from up to a dozen countries. Lt. Kulish, whose unit is now defending the city of Rubizhne, says skills including explosives handling and field tactics acquired at the exercises since 2016 helped his rapid reaction brigade fight in Donbas over recent years.

Ukrainian soldiers during training exercises in 2017 at Yavoriv.

Photo: palamar/epa-efe/rex/shutterstock/EPA/Shutterstock

Soldiers rotating out of combat in Donbas could also apply their experience in exercises and often shared lessons with their mentors. Retired U.S. Army Major General Timothy McGuire, who helped establish the Yavoriv center, in 2018 invited Ukrainian officers to observe big NATO exercises in Germany, where they watched a unit prepare a defensive position.

“I wouldn’t do it like that,” a Ukrainian general commented to Gen. McGuire, noting the troops weren’t properly camouflaged, well dispersed or sufficiently dug-in.

“It was great to see their awareness,” says Gen. McGuire. The conversation shifted to what the Ukrainians would do differently and later fed into an after-action review of the exercise.

Ukrainian troops using Western weapons to fight in the Donbas would also report back on their performance and how soldiers were integrating the arms into combat.

“It was definitely a two-way street,” says Col. Collins. “Without a doubt, we were learning from them at the same time they were learning from us.”

Ukrainian soldiers on a military vehicle in Severodonetsk on April 7.

Photo: fadel senna/Agence France-Presse/Getty Images

Away from battlegrounds, the advisers spent years pressing for the bureaucratic building blocks of a professional military, such as audit reports, professional development programs and personnel review processes—“slightly boring stuff,” says Col. Reeves. Over time, he says, selection of commanders became more meritocratic.

“Combat experience became more important than who has the biggest budget,” says Col. Reeves. “It’s not all unicorns and rainbows, but we could see how their promotion systems were being redesigned for the right reasons.”

The changes and civilian control brought layers of review that exposed corruption and waste, often sparking anger from officers and bureaucrats.

“It was kind of stressful because you were constantly creating problems,” recalls Mr. Zagorodnyuk, the former defense minister. Political will from Mr. Poroshenko and then President Zelensky kept efforts advancing.

Totalitarian states don’t have institutions empowered to challenge what the military says, Mr. Zagorodnyuk notes: “In Russia, nobody challenged the military.”

As threats from Russia increased last year, the pace of military training accelerated. British Army Major Bill Ross, who was in charge of British land-based training in Ukraine from October until this February, raced to get Ukrainian troops comfortable using NLAW antitank missiles the U.K. was shipping over. A British infantry battalion that initially planned to instruct squads of 40 Ukrainians suddenly had groups of 80, with soldiers coming from across the country.

A Ukrainian serviceman trained with the NLAW antitank missile system at the Yavoriv military base in January.

Photo: markiian lyseiko/Shutterstock

Ukrainian forces trained with the NLAW system in January.

Photo: Agence France-Presse/Getty Images

“We literally delivered those every three or four days, another course, another course, another course,” says Maj. Ross. The hope, he says, was that even if only a few hundred soldiers got trained directly, they could cascade the training to other troops. In weekly coordination meetings in Kyiv, helmed by a U.S. colonel, the Ukrainians and Western allies focused their training.

Internal opposition continued throughout. When Maj. Ross arrived at the Zhytomyr Military Institute southwest of Kyiv in October, initially he was denied access because of “an individual in their organization who did not want interference from Western troops,” he says. Eventually he reached a person who cleared his entry.

Maj. Ross’s team answered to Ukraine’s Joint Force Command, which had mapped out a defensive plan to thwart a Russian invasion. When he last saw the Ukrainian military’s slides outlining strategies in February, red arrows pointed into the country from all sides except the West. But the Ukrainians had a defensive plan.

“It was their plan,” says Maj. Ross. “We assisted.”

Write to Daniel Michaels at daniel.michaels@wsj.com