Blender Python - exporting bone matrices for animation relative to parent
After a couple of days of frustration, I have been able to get skeletal animation working fine.

Here are the details:
1] Bind pose is obtained from bone.matrix_local

bone_matrix = bone.matrix_local
if pose_bone.parent:
    bone_matrix = bone.matrix_local.inverted() *   bone.matrix_local


2] Inverse bindpose also is obtained from bone.matrix_local. But this time it is not relative to parent bone.

offset_matrix = bone.matrix_local.copy().inverted()


3] Now the animation data is obtained from pose_bone.matrix as follows:

bone_matrix = pose_bone.matrix
if pose_bone.parent:
    bone_matrix = pose_bone.parent.matrix.inverted() * pose_bone.matrix


This code works fine for me.