NAYPYIDAW, Myanmar — U Shwe Mann, a battle-scarred general, grabbed his gun and waited.

Myanmar’s first real elections in a generation were unfolding in 2015, and Mr. Shwe Mann, once the third-most powerful man in the country’s military dictatorship, was losing his bid for a seat in Parliament. The victor was a teacher from the party headed by Daw Aung San Suu Kyi, whom the ruling junta had kept under house arrest for 15 years.

Mr. Shwe Mann’s wife counseled him to pray to Buddha. The former general meditated and accepted his loss. But he still kept his gun under his pillow, just in case the people of Myanmar decided to come after him, seeking retribution for nearly 50 years of repressive rule.

No mob came for him that night, or the next. And Mr. Shwe Mann, a political chameleon, has exchanged trench warfare and the bunkered mentality of a xenophobic junta for a personal library full of titles like “101 Ways to Win an Election” and “How Successful People Think.”

Most surprising of all, he has grown close to Ms. Aung San Suu Kyi; the onetime jailer and his prisoner of conscience usually meet a couple of times a week, communing over fish noodle soup and vegetables pungent with shrimp paste. He wrote a book called “The Lady, I and Affairs of State” in which he praised her “integrity and kindness.”