Elsa Koditschek was living in a prosperous section of Vienna, near the foothills of the Alps, when the Nazis, who had annexed Austria, confiscated her home in 1940. A German officer, a squad leader in the SS, soon moved in.

Mrs. Koditschek, a Jew, was allowed to stay on, in an upstairs apartment, a tenant in her own house for about a year, until a deportation edict arrived ordering her to a bleak, uncertain future in a Polish ghetto. She fled instead, leaving behind her life’s possessions including the only major artwork she had ever purchased, a landscape by Egon Schiele.

For years, she hid in the homes of non-Jewish friends, according to an account she gave in dozens of letters written during and after the war. But she was ultimately desperate enough to seek refuge in the house the Nazis had seized from her, sneaking back in to live there in secrecy and silence with an upstairs tenant.

From there, she spied on the SS officer, Herbert Gerbing, watching through a window as he sat in the garden with his family. Probably unbeknown to her, while she hid upstairs, he was helping with the deportation of Jews across Europe.

“Who would think I would find myself sharing a roof with an SS officer?” she wrote later in a letter to her son, Paul, who had moved to New York years earlier.

Mrs. Koditschek’s Schiele was ultimately sold during the war, while she struggled to survive, and it has been sold several times since.

But her letters, handwritten on onionskin and intact after having been carefully packed away in a relative’s basement, helped the Koditschek family and researchers at Sotheby’s piece together the provenance of the painting. So this fall in New York, when it goes up for auction with an estimated value of $12 million to $18 million, Mrs. Koditschek’s heirs will share in the proceeds with its current owners.

“It’s so unusual to have a victim of Nazi theft or expropriation who writes everything down,” Lucian Simmons, the worldwide head of restitution at Sotheby’s, said. “Usually you’re trying to join the dots, but the dots are far apart.”

Mentions of the Schiele painting in the letters buttressed the provenance research by Mr. Simmons, who had approached the family in 2014 after independently finding indications that it had lost an important painting during the war. What followed were several years of negotiation with the current owners of the Schiele, Europeans who had bought it in the 1950s, that led to an agreement that will govern the sale next month of the work, “City in Twilight (The Small City II)” painted in 1913.

“It’s an important painting with a wonderful revolutionary abstract form,” Mr. Simmons said.

Perhaps more remarkable than the painting is the tale that accompanies it: the account of woman made vagabond by the Nazis who ended up returning to the very house from which she had been evicted, and living out the war there, just feet above one of her persecutors. Mrs. Koditschek survived the war, and related her account in many letters to her son, who died in 1974. But he seldom discussed those experiences in any detail, so relatives have only recently begun to unravel Mrs. Koditschek’s history by sifting through the correspondence. (Sotheby’s provided translations of excerpts from the letters.)

Their tone deepens as events evolve, according to Sarah Whites-Koditschek, a great-granddaughter, and turns grim in 1941 when the deportation order arrives. At that point, Ms. Whites-Koditschek said, “She’s just writing about whether she can find any way to escape.

Steven Luckert, a historian with the United States Holocaust Memorial Museum in Washington, said that Mrs. Koditschek’s experience stood out even among the startling tales of Jews who had lived through the war hiding in Nazi-occupied cities. “The fact that she was living in the same house as someone who was in charge of deportations makes it even more extraordinary,” he said.

Mrs. Koditschek, the widow of a banker, had sent her son and daughter away to safety before Europe became engulfed in war. But she stayed behind in Vienna, living in the three-story home her husband had built in 1911. She lived on the first floor, below her longtime tenant, Sylvia Kosminski, who was known as “Aunt Sylvia” though she was not a relative.

When the Nazi and his family took over the first floor, Mrs. Koditschek moved to the second to share quarters with Aunt Sylvia, bringing with her, the Koditschek family believes, the Schiele painting.

The letters do not indicate that Mrs. Koditschek was particularly fearful of the Nazi she was living with. He occasionally summoned her to explain how things in the house worked. She described his demeanor as civil, even after she received “an ominous card” directing her to show up at a school to be deported to German-occupied Poland. When she asked the officer if the trip could be delayed, he replied that it could not, she wrote. But he painted a glowing portrait of what life would be like in the Lodz ghetto and offered a word of advice, suggesting she bring a minimum of belongings.

“This was a kind thing for him to say,” Mrs. Koditschek wrote, “because the luggage of most Jews was robbed even before they arrived at their destination. Also of course their lives.”

It does not seem, based on her letters, that Mrs. Koditschek had an inkling of Mr. Gerbing’s larger role in the deportation of Jews. Yad Vashem, Israel’s Holocaust remembrance center, describes him as a key player in executing those policies and said he “participated in raids and arrests, and was reported to have been exceedingly brutal, at one case causing grievous injuries to detainees.”

When Mrs. Koditschek fled, she lived with a family named Heinz and spent virtually all of her time indoors, often hiding for hours behind a cupboard inside their apartment. She passed the lonely hours by practicing her English or playing chess against herself.

But her life there was disrupted in 1943, Mrs. Koditschek wrote, when Mr. Heinz came home one day “under the escort of some strange men” who began searching the apartment. She slipped away though an open door.

“I must have been wearing a magic cap of invisibility because the plainclothes men did not notice me,” she wrote, adding that as she roamed the streets that night, “people stared at me as if I was a ghost from another time.”

Under cover of darkness Mrs. Koditschek met Aunt Sylvia, and they returned to her home, rushing inside, she said in a letter, “when the coast was all clear.” For the next two years she lived a clandestine life there, sleeping on a makeshift bed and hiding whenever the doorbell rang.

Mr. Gerbing was not often home, she wrote. Historians have recorded that he had a role in deportation efforts in Paris, Slovakia and Thessaloniki. When he was away, Mrs. Koditschek noticed, Jewish laborers, marked by badges, would carry the plunder of war — furniture, a piano, clothing — into her home. “Wherever he stays,” Mrs. Koditschek wrote, “in Greece, in France, in Slovakia, he sends big boxes back with goods from each country.”

“If there was something that had to be repaired in the house,” she wrote, “the Jews came again, and they also labored in the garden.”

As compelling as the letters are, they leave much unanswered. How did Mrs. Koditschek completely avoid notice? Was Aunt Sylvia Jewish, and if so, how did she escape persecution?

Still, Ted Koditschek, Mrs. Koditschek’s grandson and a history professor emeritus at the University of Missouri, said in an interview that the correspondence is an invaluable resource for the family. “It is like a Rosetta Stone for a small group of people,” he said, adding, “There are still many questions that are unanswered and will remain that way.”

Just when the Schiele was sold is unclear, though Sotheby’s said it seems to have happened between 1941 and 1943. One of Mrs. Koditschek’s letters describes how Aunt Sylvia, who had supplied her with food while she hid, arrived one day at the Heinz apartment to say that she too now needed money and wanted permission to sell “the pictures.”

In a letter after the war, Mrs. Koditschek wrote to her son: “Aunt Sylvia sold your microscope, as well as the Schiele and the two Rugendas,” adding “Aunt Sylvia was actually repaid her loans to me twice over.”

Sotheby’s, which will earn a commission on the sale of the painting, has negotiated several similar deals between the heirs of Jews who lost art during the Holocaust and the current owners of paintings, solutions often meant to address complicated issues of ownership, ethics and international law. The auction house did not identify the current owners of the Schiele, who wanted to remain anonymous.

Mrs. Koditschek was still in her house in 1944 when the Allies bombed Vienna and in 1945 when she heard rumors that Mr. Gerbing had been killed by a mob in Prague. He never returned from that trip and the Russian army entered Vienna that year, ransacking her house, she wrote, as they passed.

Eventually, Mrs. Koditschek made her way to safety in Bern, Switzerland, where she died in 1961.

Mrs. Koditschek’s instincts about Poland probably saved her life, said Ms. Whites-Koditschek who believes her great-grandmother had somehow figured out what was happening to those people who were deported to the Lodz ghetto. “She must have heard what was happening there through her community,” she said.

Indeed, most of the Jews who lived or were shipped to Lodz went to death camps before the close of the war. By the time the Russians entered, a prewar Jewish population of about 250,000 had been reduced to fewer than 1,000.