Diacetylmorphine was first synthesized in 1874 by C.R. Alder Wright. He had been working with finding an non-addictive, or less addictive, form of opium. However, he soon found diacetylmorphine was more addictive than morphine and the drug was abandoned.
In 1897, Felix Hoffman produced two new compounds for the company he worked for, Bayer. These compounds were acetylsalicylic acid and diacetylmorphine. It should be noted that Hoffman was unaware of the work of C.R. Alder Wright.
The Bayer company observed that acetylsalicylic acid was effective in relieving minor aches and pains, while diacetylmorphine was a very effective at relieving moderate pain. As well it alleviated the symptoms of breathing disorders including asthma, bronchitis, tuberculosis.
In 1898, Bayer registered and marketed diacetylmorphine under the brand name Heroin. The word itself comes from the German word heroisch, meaning heroic. In 1899, the Bayer company marketed acetylsalicylic acid under the brand name Aspirin.
Above is a US ad from 1899, or more likely the early the 1900s. The ad includes information about both Bayer Aspirin and Bayer Heroin. Aspirin and heroin were both legally available over the counter at the time, heroin being marketed as a cough suppressant.
In the US, the Harrison Act was passed in 1914. It made heroin harder to acquire, by necessitating a prescription to obtain it. Then in 1924, importing, manufacturing, or selling heroin was outlawed completely. In 1925 heroin was made illegal in many other countries by the League of Nations.