Kazakhstan's National Security Committee (KNB) declared its "anti-terror" operation over in the financial capital after detaining the two suspects.
Three police officers and two civilians were shot dead in what President Nursultan Nazarbayev called "a terrorist act".
KNB security chief Vladimir Zhumakanov said one of the suspects may have "connected" with followers of the ultra-conservative Salafi strain of Islam while in jail on theft and weapons charges.
"We are checking this information," Zhumakanov said at a televised meeting of Nazarbayev's security council, without providing further details.
More From This Section
The first suspect is accused of killing a local resident during a carjacking and attacking the police station in broad daylight, then shooting dead a police officer and seizing his gun, the interior ministry said.
A civilian also died during the shooting at the police station.
The suspect then shot dead two officers as they gave chase, before he was arrested.
"According to preliminary information, the detained man is a 27-year-old man with previous convictions," the ministry said, adding that he was also suspected of murdering a woman at the weekend.
The health ministry said eight people had been admitted to hospital.
Two videos were widely shared on social networks showing a black-clad man roaming the streets of Almaty armed with a machinegun.
"I clearly heard shooting. Several shots were fired in quick succession. I was driving close to the police station at the time," a 40-year-old man who only gave his first name, Kanysh, told AFP.
Kazakh media reported a shootout at the national security committee building, but said the attack was repulsed and the building cordoned off.
Kazakhstan had already been on a "yellow level" threat alert after deadly shootings shook a town in its northwest in June.