Well, I'm not really really 100% sure, especially for the last part. I just tried to provide a mechanism for the transformation. The first one imitates the initial part of a Curtius rearrangement, while the latter part involves a [3,3]-sigmatropic rearrangement. I am not 100% sure of the last part, however. But it seems possible for it to do that.
What's bothering me is the fact that the Nitrogen atom in the amide [in this case, the lactam] is not sufficiently basic enough, since it's included in the resonance. I just forced it to take the hydrogen atom to produce that product.
I've read a book about polarophile interactions (I believe this is still under pericylic reactions), and maybe the explanation for the last part lies in that. The book that I was referring to was Dr. Grossman's book on The Art of Writing Reasonable Organic Reaction Mechanisms (2e). Anyway, I hope that helped you in a small way.