Media playback is not supported on this device
Elis Ligtlee of the Netherlands led from the front to win gold, with Australian Anna Meares in third.
James, 24, missed the London Games and then had the best part of 18 months out with injury and health issues.
"I was so desperate for that medal," the Welsh star told BBC Sport. "I can't believe it happened."
She was last with one lap to go but found a way around the outside to force her way into second on the line to win Britain's fourth medal on the track.
It was a second medal in two Olympic kierins for GB after Victoria Pendleton won the event in London, where it made its debut.
The silver medal came just minutes after Laura Trott became the first British woman to win three gold medals as she won team pursuit gold with Joanna Rowsell-Shand, Elinor Barker and Katie Archibald in a new world record time.
Abergavenny-born James, who was watched by her boyfriend George North, the Wales and British and Irish Lions rugby player, told BBC One: "I came in with good form and I love the keirin and I love racing.
"Once I got through the first round, I just wanted to enjoy the day.
"It was so special, I have 10 of my family here. My mum has never been on a long-haul flight before.
"I have had so much support from British Cycling, it has been a tough two years and I couldn't have done it without everyone's help."
North later tweeted: "Words can't describe how proud I am! Everything she's been through. What an absolute hero."
James underwent an operation in 2014 to remove potentially cancerous cells following a routine smear test.
That scare was followed by keyhole surgery on a shoulder injury and then months of rehabiltation for a long-term knee injury.

He will take over from Sir Iain Lobban when he steps down this autumn after six years as director, Foreign Secretary William Hague announced.
Mr Hannigan, 49, has been director general of defence and intelligence at the Foreign Office since 2010.
Cheltenham-based GCHQ came under scrutiny after disclosures from ex-US intelligence contractor Edward Snowden.
Mr Hague said: "As well as his impressive personal qualities, Robert brings to the job a wealth of relevant experience in the fields of national security, counter-terrorism and international relations.
"I'd also like to thank Sir Iain Lobban for his consistently strong and professional leadership as director of GCHQ since 2008."
Mr Hannigan's appointment has been agreed with Prime Minister David Cameron in consultation with Deputy Prime Minister Nick Clegg.
Gloucestershire-born Mr Hannigan, a married father of two, has advised the prime minister on counter-terrorism, intelligence and security policy for a number of years.
He said of his appointment: "It is a privilege to be asked to lead GCHQ, an organisation which is so central to keeping the people of this country safe.
"I have great respect for the integrity and professionalism of the staff of GCHQ and for what they have achieved under the outstanding leadership of Iain Lobban.
"I am excited about meeting the challenges of the coming years with them."
Mr Hannigan previously worked as principal adviser to then prime minister Tony Blair on the Northern Ireland peace process.
He was also responsible for the UK's first cyber security strategy and oversaw the first national security strategy, and has chaired Cobra emergency committee meetings on terrorist incidents.
National Security Adviser Sir Kim Darroch, who led the recruitment process, said: "I am delighted Robert Hannigan has been appointed director, GCHQ.
"He will bring energy, flair, deep knowledge and extensive experience to the role, and I look forward to working closely with him."
Sir Kim thanked Sir Iain for doing "an absolutely outstanding job at an exceptionally testing time for the organisation".
Sir Iain joined GCHQ in 1983, becoming director in June 2008.
Last November, he became the first head of the agency to give evidence in public when he appeared before MPs on the Intelligence and Security Committee, alongside the heads of MI5 and MI6.
They came under pressure to be more open after leaks by Mr Snowden revealed widespread spying by GCHQ and the US National Security Agency.
Sir Iain told the committee Mr Snowden's disclosures had done immense damage to Britain's counter-terrorism efforts.
Britain's Becky James bounced back from a serious knee injury and a cancer scare to take silver with a late surge in the Olympic women's keirin in Rio.
[NEXT_CONCEPT]
Robert Hannigan has been named as the new head of the UK's electronic surveillance agency GCHQ.