In his first fight with Bane, 1) Batman subconsciously wants to die, and 2) Bane is more motivated.
Others have noted some of the tactical reasons why Batman lost the first fight and won the second. That's definitely part of it, but I'm going to argue for a more fundamental reason.
At the beginning of the film, Gotham is at a point where it apparently does not need Batman (the Dent Act led to the end of organized crime in that city). That is what Bruce wanted, since if he could stop being Batman he could pursue a relationship with the woman he loves, Rachel. With Rachel's death, he no longer has a reason to not be Batman, but Gotham also does not need Batman (at least it appears this way at the beginning of the film).
Given those things, Bruce no longer has a sense of purpose, which is evidenced by his reclusiveness and neglect of Wayne Enterprises.
Bane's emergence seems to give Bruce a reason to be Batman again, but he doesn't have an "exit plan," as his did before.
WAYNE: Rachel died knowing we'd decided to be together. That was my life beyond this cave and I can't just move on. She didn't. She couldn't.
Alfred also worries about this:
WAYNE: That's what you're afraid of - that if I go back out there I'll fail.
ALFRED: No. I'm afraid that you want to.
So, my theory is that Bruce subconsciously wants to die. I don't think that he wants Bane specifically to kill him, but he's okay with being killed as Batman in one way or another. Bane even notes this:
BANE: You don't fear death. You welcome it. Your punishment is to be more severe.
After Bruce loses to Bane and is in prison, he is annoyed by the fact that he wasn't killed. He asks Bane:
WAYNE: Why didn't you just kill me?
He even asks the other prisoners to kill him:
PRISONER: He asks if you would pay us to let you die. I told him you have nothing.
WAYNE: Do it for the pleasure.
At this point in the film he really wishes he would have just been killed in his fight with Bane.
So that explains the first part of the question.
As to the second part...
Batman wins the second fight because 1) he learns to fear death, 2) his desire to save Gotham as Batman is rekindled, 3) "falling" and "rising again" made him stronger.
In prison, the TV images of what's occurring in Gotham motivate Bruce to recover from his injuries and train. He's noticeably more driven than before. He sees Ra's al Ghul in a hallucination, who taunts him about his failure to save Gotham. This gives him additional motivation and purpose:
PRISONER: Why build yourself?
WAYNE: I'm not meant to die in here.
The film makes it clear that Bruce needs to learn to fear death. Batman can't make the "jump" without fear. With the safety rope, he doesn't have the appropriate fear of death. Without it, he does have the right amount of fear, which motivates him to make the jump.
WAYNE: I do fear death. I fear dying in here while my city burns with no one there to save it.
Presumably this fear of death, his anger over what Bane did to Gotham, and his training let Bruce beat Bane the second time around.