Japanese police have detained a man who had holed up in a building after allegedly killing four people, including two police officers, in a gun and knife attack.

Masanori Aoki, 31, was taken into custody at his house outside a farm near the city of Nakano in the Nagano region, police said.

They also confirmed a fourth fatality overnight - an elderly woman who was found injured at the scene and later pronounced dead.

Another woman and two police officers were earlier confirmed dead in the attack.

Mr Aoki has initially been formally arrested with suspicion of murder over the death of one of the police officers, Nagano regional police chief Iwao Koyama told reporters.

He is likely to be rearrested later in connection with the other deaths, in a process that allows law enforcement to detain a suspect for longer while investigations continue.

The attack in a rural area of the central region was a rare instance of violent crime in Japan, which has a low murder rate and some of the world's toughest gun laws.

Several local media reported that Mr Aoki is the son of the speaker of Nakano's city assembly, and lived with his parents and aunt in the house where he holed up overnight.

"We pray for the souls of the deceased and express our heartfelt condolences to the bereaved families," top government spokesman Hirokazu Matsuno told reporters.

"Police are investigating to uncover the whole picture of the incident, including how the crime developed and its background."