The gunman fired shots “wildly” around the amphitheatre at around lunchtime, a police spokesman told AFP.
The shooting took place during an organic chemistry class for bioscience students, a course the assailant himself was enrolled in, university president Bernhard Eitel told reporters.
The motive for the attack was not immediately clear but police said there were early indications the gunman had previously suffered from psychiatric problems.
The assailant had sent an alarming text message to his father shortly before the rampage.
In the WhatsApp message, the gunman wrote that “people have to be punished now”, Mannheim police chief Siegfried Kollmar told a press conference.
The message also said: “He doesn’t want to be buried in a cemetery, but at sea.”
The shooting shocked the picturesque town of Heidelberg and left students traumatised.
Thirty people were attending the lecture at the Neuenheimer Feld campus when the gunman burst in and fired multiple shots using “a long weapon”, Kollmar said.
Police received the first emergency calls from inside the lecture hall at 12:24 pm and officers were at the scene within 10 minutes, he added.
They discovered the gunman’s body outside the building, after he killed himself.
The rampage left four people wounded, including a young woman, 23, who died of her injuries in hospital several hours later.
The three others suffered wounds to the legs, back and face, Kollmar said.
The shooting triggered a major police operation at the university’s Neuenheimer Feld campus, with police on Twitter urging people to steer clear of the area “so that rescue workers and emergency services can travel freely”.
Police deployed sniffer dogs around the campus, and investigators were seen examining a rifle lying next to a beige backpack.
The university’s Neuenheimer Feld campus, shown below in the map, hosts natural sciences departments, part of the university clinic as well as a botanical garden.
‘My heart breaks’
Chancellor Olaf Scholz voiced shock at the assault, and said his thoughts were with the victims and their relatives. “It breaks my heart to hear such news,” he told journalists.
Kollmar said the gunman, who was not previously known to police, had acted alone and that he apparently bought the weapons abroad.
The assailant had carried two rifles onto the campus, said Kollmar, adding that investigators found more than 100 rounds of ammunition in the backpack he was carrying.
Officers have searched the gunman’s home in the nearby city of Mannheim, he added.
The medieval town of Heidelberg in the state of Baden-Württemberg has a population of 160,000 people.
Heidelberg University, founded in 1386, is Germany’s oldest university and one of the most prestigious in Europe.
The state’s interior minister, Thomas Strobl, expressed his sympathies for those affected by the “terrible event” and urged students in distress to make use of the mental health support on offer.
“Universities and the city of Heidelberg will remain spaces free of fear where science can flourish and where young people can prepare for the rest of their lives,” he said.
The university resumed in-person classes in October after months of distance learning because of the coronavirus pandemic.
Students have to show they are vaccinated against Covid, recovered or in possession of a recent negative test if they want to enter university buildings.
Tightened gun laws
Germany has been hit in recent years by a spate of attacks, mostly perpetrated by jihadists or far-right militants.
School shootings however are relatively rare in Germany, a country with some of the strictest gun laws in Europe.
In 2009, a former pupil killed nine students, three teachers and three passers-by in a school shooting at Winnenden, also in Baden-Württemberg. The gunman then killed himself.
In 2002, a 19-year-old former student, apparently in revenge for having been expelled, gunned down 16 people including 12 teachers and two students at a school in the central German city of Erfurt. He too then killed himself.
Both massacres were carried out with legal weapons and spurred Germany to tighten its gun laws.
The country currently requires anyone younger than 25 to pass a psychiatric exam before applying for a gun licence.
With reporting by Michelle Fitzpatrick and Sebastien Ash