US President Barack Obama and congressional leaders have struck a last-minute budget deal to narrowly avert a government shutdown.